Skip to content

Commit

Permalink
Introduce type of indexing into a tuple.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zemnmez committed Dec 17, 2024
1 parent 3dbc8e0 commit 402f912
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ts/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,16 @@ export type Tuple<T, N extends number> = N extends N
type _TupleOf<T, N extends number, R extends unknown[]> = R['length'] extends N
? R
: _TupleOf<T, N, [T, ...R]>;

type StringToNumber<S extends string> =
S extends `${infer N extends number}` ? N : never;


type TupleIndexStrings<N extends number>
= keyof Omit<
Tuple<unknown, N>,
keyof []
>

export type TupleIndex<N extends number> = TupleIndexStrings<N> extends string ?
StringToNumber<TupleIndexStrings<N>> : never;

0 comments on commit 402f912

Please sign in to comment.