I am not able to understand the code that I saw in a library which creates Tuple type.
type _TupleOf<T, N extends number, R extends unknown[]> = R['length'] extends N
? R
: _TupleOf<T, N, [T, ...R]>;
export type Tuple<T, N extends number> = N extends N
? number extends N
? T[]
: _TupleOf<T, N, []>
: never;
And it is used
Tuple<string, 10>
I am new to typescript. But this is what I understood.
In Tuple type
I can't understand what N extends N does here since it will always be true.
number extends N checks whether it's literal type or number type based on that, it will either return either an array or a tuple.
For example:
Tuple<string,number> //string[]
Tuple<string,2> // [string,string]
And In _TupleOf
R['length'] extends N checks the size is the same as N or not.
I can't understand what N extends N does in Tuple type.