Skip to content

Bug with mapped types and indexing#17238

@SimonMeskens

Description

@SimonMeskens

Minimal repro:

typeAB={a: 'a'b: 'a'}typeT1<KextendskeyofAB>={[keyinAB[K]]: true}typeT2<KextendskeyofAB>=T1<K>[K]// BUG: should be an error for K = 'b'

I came across this in production code and made a contrived example to show the issue. Might be related to #15756.

Note that there might be two bugs here in play, one with mapped types and one with lookup types on mapped types. Once I start pushing the types a little, I come across a wild range of unexpected behavior.

Pay attention to the last type 'G', where the compiler is suddenly correct again, which is probably the oddest one of all.

// Let's make a simple typetypeStringA="a"typeA={a: StringAb: StringA}// so type A[S] should always resolve to "a" for any valid keys// note that it doesn't collapse T, even though it couldtypeT<Sextends"a"|"b">=A[S]// expands A, but doesn't collapse the indexerlett1: T<"a">// t1: "a"lett2: T<"b">// t1: "a"// Let's make a silly generic type that only keeps key "a"// The compiler correctly infers that this new type is just{a: true }typeB<Sextends"a"|"b">={[keyin"a"]: true}// B ={a: true }// Let's make the type more generic, remember that A[S] is actually just "a"// The compiler now infers that the type of C is{}, this is plain wrongtypeC<Sextends"a"|"b">={[keyinA[S]]: true}// C ={}// Obviously, we couldn't do a lookup on such a type// Compiler correctly complains that we can't index an empty objecttypeD<Sextends"a"|"b">={}[S]// Type 'S' cannot be used to index type '{}'.// Now let's add a lookup to the generic version// Curiously, it doesn't fail this time, but infers 'true', how oddtypeE<Sextends"a"|"b">={[keyinA[S]]: true}[S]// = true// It even happens when we index the above type C, which the compiler claims is{}typeF<Sextends"a"|"b">=C<S>[S]// = true// Let's also try to use this type, it will become important later// This does exactly what's expectedleta1: F<"a">// a: trueletb1: F<"b">// b: true// Now let's make it even weirder and add a general indexer// This correctly doesn't fail as any key is now valid// The type it infers though, is just plain weird:// G = ({} &{[key: string]: false})[S]typeG<Sextends"a"|"b">=(C<S>&{[key: string]: false})[S]// And now the weirdest thing of all, let's use this type// It correctly infers the result! Both of these are correct!leta2: G<"a">// a: trueletb2: G<"b">// b: false

This has to be the weirdest bug I've run across

Metadata

Metadata

Assignees

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