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
{{ message }}
Repository navigation
Allow inferring rest element types in conditional types involving tuples #25719
However it is not possible to infer the type of the remaining arguments in one go, except by resorting to functions:
typeRestArgs<Textendsany[]>=Textends[any, infer R[]] ? R : // this does not work - no way to specify that R should be an array!Textends[any] ? []] :
never;// this doestypeRestArgs<Textendsany[]>=((...args: T)=>void)extends((first: any, ...rest: infer S1)=>void) ? S1
: Textends[infer S2] ? []
: Textends[] ? []
: never;typeT1=RestArgs<[1,2,3]>;// [2, 3]typeT2=RestArgs<[1,2]>;// [2]typeT3=RestArgs<[1]>;// []typeT4=RestArgs<[]>;// []
I would like to see the possibility to infer rest types in tuples, e.g. like this (square brackets):
typeRestArgs<Textendsany[]>=Textends[any, infer R[]] ? R : never;
or like this (3 dots)
typeRestArgs<Textendsany[]>=Textends[any, infer ...R] ? R : never;
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript / JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. new expression-level syntax)
I just ran into this issue myself and wanted to throw in my two cents about the syntax for how this would behave. Considering how this inference is written for function types and how rest elements of tuples are constructed:
typeTail<T>=Textends(head: any, ...tail: infer U) ? U : never;typeRest=string[];typeTuple=[any, ...Rest];
I would suggest the following syntax:
typeTail<T>=Textends[any, ...infer U] ? U : never;
Reacted by Alec Larson, Augusto Moura, Elias Toivanen, Zoey and Randy Sugianto (Yuku)
Activity
jscheiny commentedon Jul 25, 2018
I just ran into this issue myself and wanted to throw in my two cents about the syntax for how this would behave. Considering how this inference is written for function types and how rest elements of tuples are constructed:
I would suggest the following syntax:
aleclarson commentedon Oct 27, 2018
The current workaround:
Note: Does not work in TypeScript 2.9.x and under.
darcyparker commentedon Jun 11, 2019
AlCalzone - Thanks for the examples
FirstArgandRestArgs. They inspired these additional examples:Based on the above, it would be great to have something like:
smcatala commentedon Aug 14, 2019
chocolateboy commentedon Nov 14, 2020
This works in TypeScript v4 (via variadic tuple types):
ethanresnick commentedon Sep 19, 2023
This seems like it can probably be closed, given the snippet shown above?
futtetennista commentedon Jan 29, 2024
Not sure that works but I might be missing something here. For example
Playground link
ethanresnick commentedon Jan 29, 2024
Stefano (@futtetennista) It fails in your example because
typeof xsis not a tuple type (it'sstring[]). Here is an updated version that works: