Skip to content

Mapped type indexed access issue #47368

Closed
@ahejlsberg

Description

@ahejlsberg

Bug Report

An indexed access type { [P in K]: E }[X] should be assignable to an instantiation of E with X substituted for P.

This issue was originally reported here.

🔎 Search Terms

Mapped type indexed access.

🕗 Version & Regression Information

Nightly build

⏯ Playground Link

Playground link with relevant code

💻 Code

type ArgMap = { a: number, b: string };
type Func<K extends keyof ArgMap> = (x: ArgMap[K]) => void;
type Funcs = { [K in keyof ArgMap]: Func<K> };

function f1<K extends keyof ArgMap>(funcs: Funcs, key: K, arg: ArgMap[K]) {
    funcs[key](arg);
}

function f2<K extends keyof ArgMap>(funcs: Funcs, key: K, arg: ArgMap[K]) {
    const func = funcs[key];  // Type Funcs[K]
    func(arg);
}

function f3<K extends keyof ArgMap>(funcs: Funcs, key: K, arg: ArgMap[K]) {
    const func: Func<K> = funcs[key];  // Error, Funcs[K] not assignable to Func<K>
    func(arg);
}

🙁 Actual behavior

Unexpected error in example above.

🙂 Expected behavior

No errors in example above.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions