TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

`instantiateMappedTupleType` should not use array index as mapping key, at least it's not in an appropriate way

Open imhele opened this issue 1 year ago • 0 comments

🔎 Search Terms

instantiateMappedTupleType

🕗 Version & Regression Information

  • This is the behavior in every version I tried.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.0-dev.20240824#code/C4TwDgpgBA0hIGcA8AVAfFAvFA3lA2jFAJYB2UA1vAPYBmUKAugFyxQC+A3AFCiRQAxatVRQIAD2ARSAEwRQAhqRD5GGbHERJ8CYACcyAcwA0UXQdImoAOlspT5o2s5QA9K4IAiAAyfTngEY-G1tNZHRTUgBXAFsAIwg9Rm4UvmgAQSxBYW1HS2c3D3wfYMDS30jYhKTecGgAISyw3P0jB1bLU1trHQ7DRnaLfrQXdy8KqDL-ACZSgGZPZKA

💻 Code

type Keys<T> = { [K in keyof T]: K };
type Foo<T extends any[]> = Keys<[string, string, ...T, string]>; // ["0", "1", ...Keys<T>, number]


type A = Foo<[string]>; // ["0", "1", "0", number]
type B = Keys<[string, string, ...[string], string]>; // ["0", "1", "2", "3"]

🙁 Actual behavior

After instantiating the Keys<[string, string, ...T, string]> to ["0", "1", ...Keys<T>, number] in advance, the position of T is lost, resulting in incorrect indexes.

🙂 Expected behavior

Option 1: look up real indexes during instantiating.

type Foo<T extends any[]> = Keys<[string, string, ...T, string]>; // ["0", "1", ...Keys<T>, ...Keys<[string]>]
type A = Foo<[string]>; // ["0", "1", "2", "3"]

Option 2: don't use indexes

type Foo<T extends any[]> = Keys<[string, string, ...T, string]>; // [number, number, ...Keys<T>, number]
type A = Foo<[string]>; // [number, number, number, number]

Additional information about the issue

No response

imhele avatar Aug 24 '24 10:08 imhele