You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When accessing the items of this array (items[0]), they are "resolved" to the type defined for the generic, e.g., unknown in this example, but should be kept unresolved so that they show up as T[number] | undefined.
This becomes an issue once a function takes more than one array like this, for example, if it wants to use the tuple/array shape as a param in the output. This can lead to real runtime errors where TypeScript isn't providing enough safety.
This is also relevant for for-of loops iterating over items
for(constitemofitems){console.log(item);// ^? unknown, can be T[number]}
π Motivating Example
The following example passes typescript checking but fails in runtime and throws exceptions when trying to access methods of strings on a number, and vice-versa.
typeDoubleMap<Textendsreadonlyunknown[],Sextendsreadonlyunknown[],RT,RS,>=[...{[IinkeyofT]: RT}, ...{[IinkeyofS]: RS}];functiondoubleMap<Textendsreadonlyunknown[],Sextendsreadonlyunknown[],RT,RS,>(t: T,s: S,mapperT: (item: T[number])=>RT,mapperS: (item: S[number])=>RS,): DoubleMap<T,S,RT,RS>{constoutput: (T[number]|S[number])[]=[];for(constitemToft){// Oops, we got the mappers wrong, we are calling mapperS with an item from Toutput.push(mapperS(itemT));}for(constitemSofs){// ...And here we are calling mapperT with an item from Soutput.push(mapperT(itemS));}// @ts-expect-error [ts2322] -- TypeScript can't tell we finished building the output, this is fine...returnoutput;}constresult=doubleMap(// ^? [boolean, boolean, boolean, number, number, number]["a","b","c"]asconst,[1,2,3]asconst,(item)=>item.startsWith("a"),(item)=>item.toPrecision(1),);
The text was updated successfully, but these errors were encountered:
eranhirsch
changed the title
Tuples: maintaining type-param when accessing items of tuple type-param
Tuples: maintaining type-param in the type of it's items
May 22, 2025
Similar suggestion in #33181: currently indexing into generic objects with non-generic keys prematurely widens the generic to its constraint. In this case the key is number or a numeric literal type.
Uh oh!
There was an error while loading. Please reload this page.
π Search Terms
for-of, T[number], tuple (I couldn't find more specific search terms).
β Viability Checklist
β Suggestion
When a function takes an array/tuple as a type-parameter, e.g.
When accessing the items of this array (
items[0]
), they are "resolved" to the type defined for the generic, e.g.,unknown
in this example, but should be kept unresolved so that they show up asT[number] | undefined
.This becomes an issue once a function takes more than one array like this, for example, if it wants to use the tuple/array shape as a param in the output. This can lead to real runtime errors where TypeScript isn't providing enough safety.
This is also relevant for for-of loops iterating over items
π Motivating Example
The following example passes typescript checking but fails in runtime and throws exceptions when trying to access methods of strings on a number, and vice-versa.
https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&noUnusedLocals=true&noUnusedParameters=true&noPropertyAccessFromIndexSignature=true&noImplicitOverride=true&noFallthroughCasesInSwitch=true&exactOptionalPropertyTypes=true#code/C4TwDgpgBAIg9gVwEYBsIFkCGYA8AoKKAFSggA9gIA7AEwGcoAnCTGuKlEKBKgayrgB3KgG0AugBoCUAMqkK1ekxZsOXHvyGjJ0gEpEphXTKkA+KAF4oIgHR2A3tYCSUAJZUovCCDgAzYmIAXFD6UAC+ElB2No4iLu6e3n6yQSFyYWIA3Hh4vjwAxsCu7FBsyGhYuNIk5JS0DMys7JzcfALC4oay8nVKjaotGu3aXfqjJnimABTSwMEG0nTBE4QAttiQjETBU66Uq-MiVAirSBCMYgCUluZj0utgmzI7exAHskcnZxfXFrcTl2C8HKGGwOAMskiYzS5ns0ny7DowCgiGAYAQcygUyIn1O5zEUAAPh9jnifuJLNYsjlCL44IwsQiqEi3PsSMlgNc4YRCAB6XlQADycDAdEigmgAHM4MjgAALaAPTYMQSMdiS8XQTDMKD5TAoFDuSVQJXnOSCPZyqCYDyvVZQXxq+1EaSEVHo4A2dF0OVTU2MGS7NmXS7ZQhhGkO+mMxHIu1yZJ0LmuqD8qJ2ACCtCgCp1EutOr1BqNJo25xIFvl1tt+wdTtkKfdGK9CB9frLWyDbxkIbD4UjaYAAsA6ABaciQQrjxhqhkiEcAJgAzAuFwTR6PiOAIDJ8oxXGBkXqqAByWUQA1QfO+dyuH0QGhQJAIVwoGgl+XQJvASLyu9uBgbyoCBomkZhgAQRgPG-bIIzwJkWWYOgEBQZErDKVBQTAGY+QFKAAD0AH5pBEAAiTBSMiUikEoqBSPyUiCUwBgEJ-EiAEZIgXSIlyYljYy6LtVl+cw7RsJFtRHAB1S0pnI0jLkEu0RNZN4bGAOAAAVmHyO9iioKZ2MUvBQzwIA
Live example:
https://codesandbox.io/p/sandbox/tgr8pm
π» Use Cases
The text was updated successfully, but these errors were encountered: