Description
case 1
export function test<T extends object, P extends keyof T>(
whatever: T, name: P
): T[P] {
const result = Math.random() > 0.5 ? whatever[name] : 'Hello!'; // <-- expected T[P] | string, actual T[P]
return result; // <-- returning unsound result
}
case 2
export function oneOrAnother<A, B>(one: A, another: B): A | B {
return Math.random() > 0.5 ? one : another;
}
export function test<T extends object, P extends keyof T>(
whatever: T, name: P
): T[P] {
const result = oneOrAnother(whatever[name], 'Hello!'); // <-- result is clearly string | T[P] as expected
return result; // <-- expected a type error, actual returning string | T[P] instead of T[P] no problem
}