Skip to content

Cannot use mapped types to create new mapped types #48246

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
libre-man opened this issue Mar 14, 2022 · 3 comments · Fixed by #48273
Closed

Cannot use mapped types to create new mapped types #48246

libre-man opened this issue Mar 14, 2022 · 3 comments · Fixed by #48273
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@libre-man
Copy link

libre-man commented Mar 14, 2022

Bug Report

Cannot use mapped types to create new mapped types. I have no idea why this would happen because the types are computed correctly.

🔎 Search Terms

  • mapped types

🕗 Version & Regression Information

This changed between version 4.5.5 and 4.6.2.

⏯ Playground Link

Playground link with relevant code

💻 Code

function makeCompleteLookupMapping<T extends ReadonlyArray<any>, Attr extends keyof T[number]>(
    ops: T,
    attr: Attr,
): {
	[Item in T[number]as Item[Attr]]: Item;
} {
    return Object.fromEntries(
        ops.map(op => [op[attr], op] as const),
    );
}

const ALL_BARS = [
	{ name: 'a'},
	{name: 'b'},
] as const;
const BAR_LOOKUP = makeCompleteLookupMapping(ALL_BARS, 'name')
type BarLookup = typeof BAR_LOOKUP;

type Baz = { [K in keyof BarLookup]: BarLookup[K]['name']; };

🙁 Actual behavior

I cannot use name to index BarLookup[K] where K is a keyof BarLookup.

🙂 Expected behavior

I expect type Baz to be {a: 'a', b:'b'}.

@Andarist
Copy link
Contributor

This looks strange to me too - especially given that this worked before. As a temporary workaround you can use a Prop helper, like here.

What I've also noticed is that when indexing this BarLookup[K][/*HERE*/] we get string methods in the autocomplete as if would think that the type of BarLookup[K] is a string or something

@RyanCavanaugh
Copy link
Member

Bisected to #47370

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Mar 14, 2022
@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Mar 15, 2022
@ahejlsberg ahejlsberg added this to the TypeScript 4.7.1 milestone Mar 15, 2022
@ahejlsberg
Copy link
Member

This and #48157 are caused by the same core issue. We need to exclude mapped types with optionality modifiers and/or as clauses from the new constraint logic introduced in #47370.

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
5 participants