Skip to content

Mapped type indexed access issue #47368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ahejlsberg opened this issue Jan 10, 2022 · 0 comments · Fixed by #47370
Closed

Mapped type indexed access issue #47368

ahejlsberg opened this issue Jan 10, 2022 · 0 comments · Fixed by #47370
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@ahejlsberg
Copy link
Member

ahejlsberg commented Jan 10, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants