Repository navigation
Key in mapped variadic tuple should not be literal after the variadic part #48856
Description
Activity
I don't think this is a bug. Consider:
type Box<T> = { value: T }; type Boxified<T> = { [P in keyof T]: Box<T[P]> }; type T0 = Boxified<[number, string, ...boolean[]]>; // [Box<number>, Box<string>, ...Box<boolean>[]]
With the proposed change,
Pwould benumberinstead of"2"for the rest element, which means the resulting rest element would be...Box<string | number | boolean>[]instead of just...Box<boolean[]>. Not only is that less correct, it is a breaking change. Effectively,numberis a less precise way of expressing "element at index 2 or above", whereas"2"gets much closer. This speaks to the fact that in homomorphic mappings over array and tuple types, the key types are just exemplars and not an exact representation of some specific property.- addedWorking as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bugand removedBugA bug in TypeScriptA bug in TypeScript
on Apr 27, 2022 Josh-Cena commented
on Apr 28, 2022 ContributorAuthorMore actionsI also thought about that. The problem is it's really hard to be correct for both the key type itself, and for homomorphic mapped types to continue be homomorphic. However, accessing
T[P]andPdirectly should be two orthogonal actions. In my actual case, I'm doingEntriesinstead ofKeys:export type Entries<O extends unknown[]> = { [K in keyof O]: [K, O[K]] };
In an ideal world, I'd like
Entries<[number, string, ...boolean[]]>to be[["0", number], ["1", string], ...[number, boolean][]]. Does that sound reasonable?If we have subtracted types, the index should rather by
Exclude<number, 0 | 1>, but since we are not there yet, I'd be more happy withnumberthan2as the index type.typescript-bot commented
on Apr 30, 2022 ContributorMore actionsThis issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
- addedFix AvailableA PR has been opened for this issueA PR has been opened for this issue
on Jan 11, 2024 Joshua Chen (@Josh-Cena) FWIW, I now agree with your observations and suggested changes. See #57031. Better late than never! 😄
Reacted by Mateusz Burzyński, Andrii Dieiev, Joshua Chen and andrew jarrettReacted by Joshua Chen and andrew jarrett- locked as resolved and limited conversation to collaborators
on Oct 22, 2025
Bug Report
🔎 Search Terms
Variadic tuple mapped type keyof
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
AandBboth have the variadic part typed as..."1"[], which is wrong.Bis further wrong because thenumberpart becomes"2", while in practice it would rarely be"2".🙂 Expected behavior
Ashould be["0", ...number[]];Bshould be["0", ...number[], number]