Skip to content

Cannot use mapped types to create new mapped types #48246

Closed

Description

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'}.

Activity

  1. Andarist commented on Mar 14, 2022

    @Andarist
    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

  2. RyanCavanaugh commented on Mar 14, 2022

    @RyanCavanaugh
    Member

    Bisected to #47370

  3. added
    Needs Investigation
    on Mar 14, 2022
  4. added
    Bug
    and removed
    Needs Investigation
    on Mar 15, 2022
  5. ahejlsberg commented on Mar 15, 2022

    @ahejlsberg
    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.

  6. added
    Fix Available
    on Mar 15, 2022
  7. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugFix Available

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions