Skip to content

Instantly share code, notes, and snippets.

@rauschma
Created January 25, 2025 21:46
Show Gist options
  • Save rauschma/698990c9425f97a7782fea082aa4285c to your computer and use it in GitHub Desktop.
Save rauschma/698990c9425f97a7782fea082aa4285c to your computer and use it in GitHub Desktop.
type CreateTuple<Len extends number, Acc extends unknown[] = []> =
Acc['length'] extends Len
? Acc
: CreateTuple<Len, [...Acc, true]>;
;
type Length<Tup extends Array<unknown>> =
Tup['length']
;
type Unshift<Tuple extends Array<unknown>, Value> =
[Value, ...Tuple]
;
type _1 = Unshift<['b', 'c'], 'a'>; // ["a", "b", "c"]
type Rest<Tup extends Array<unknown>> =
Tup extends [unknown, ...infer Rest]
? Rest
: Tup
;
type _2 = Rest<['a', 'b', 'c']>; // ["b", "c"]
type Inc<Num extends number> =
Length<Unshift<CreateTuple<Num>, true>>
;
type _3 = Inc<5>; // 6
type Dec<Num extends number> =
Length<Rest<CreateTuple<Num>>>
;
type _4 = Dec<5>; // 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment