I have read this answer https://stackoverflow.com/a/45486495/1108891, which demonstrates tuple type inference. After some experimentation, I came across this scenario.
When our tuple
function has T extends string[]
, the tuple has string literal types.
export const tuple_0 = <T extends string[]>(...args: T): T => args;
const ALL_SUITS_0 = tuple_0('hearts', 'diamonds', 'spades', 'clubs');
type T0 = typeof ALL_SUITS_0; // ["hearts", "diamonds", "spades", "clubs"]
On the other hand, with T extends any[]
, the tuple has string types (not literals).
export const tuple_1 = <T extends any[]>(...args: T) => args;
const ALL_SUITS_1 = tuple_1('hearts', 'diamonds', 'spades', 'clubs');
type T1 = typeof ALL_SUITS_1; // [string, string, string, string]
Why do we lose the literal types in the latter case?
I suspect this has something to do with how many specificity steps the type inferences allows itself to take. That is, any
is one step away from string
, and string
is one step away from 'some-string-literal'
. Does the type inference only allow itself to take one step?