Created
January 25, 2025 21:46
-
-
Save rauschma/698990c9425f97a7782fea082aa4285c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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