Closed
Description
TypeScript Version: 2.7.0-dev.20180123
Search Terms: indexed access, type parameter keyof
Code
interface I {
foo: string;
}
declare function take<T>(p: T): void;
function fn<T extends I, K extends keyof T>(o: T, k: K) {
take<string>(o[k]); // Argument of type 'T[K]' is not assignable to parameter of type 'string'.
take<{} | null | undefined>(o[k]); // Type 'T[K]' is not assignable to type '{} | null | undefined'. Type 'T[K]' is not assignable to type '{}'.
take<any>(o[k]); // this one works
}
Expected behavior:
Compiles without error. 2.7.0-rc allows all of the above, although the type of the indexed access is not correctly inferred, see #12991 (comment)
Actual behavior:
Error on the first two calls, see inline comments.
Playground Link:
Related Issues:
#12991