-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Regression: IndexedAccess T[keyof T] is not assignable to anything #21368
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Milestone
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue