Skip to content

Regression: IndexedAccess T[keyof T] is not assignable to anything #21368

Closed
@ajafff

Description

@ajafff

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions