Created
September 19, 2020 05:37
-
-
Save duarten/5dd8bd7caedf33283b3618cc3de8230a to your computer and use it in GitHub Desktop.
Compile-time permutations in TypeScript
This file contains hidden or 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 Access<Tuple extends readonly unknown[], I extends string> = | |
I extends keyof Tuple ? Tuple[I] : never | |
type Swap<Tuple extends readonly unknown[], I extends string, J extends string> = { | |
[Index in keyof Tuple]: Index extends I ? Access<Tuple, J> : Index extends J ? Access<Tuple, I> : Tuple[Index] | |
} | |
type Length<Tuple extends readonly unknown[]> = Tuple["length"] extends number ? `${Tuple["length"]}` : never | |
type PushFront<Tuple extends readonly unknown[], E> = [E, ...Tuple] | |
type Range<Size extends string, Tuple extends readonly unknown[] = []> = | |
Length<Tuple> extends Size ? Tuple : Range<Size, PushFront<Tuple, 0>> | |
type Increment<I extends string> = Length<PushFront<Range<I>, 0>> | |
type FixedIdx<Tuple extends readonly unknown[], I extends string, Idx extends string> = | |
I extends Length<Tuple> ? Tuple : Perms<Swap<Tuple, I, Idx>, Increment<Idx>> | FixedIdx<Tuple, Increment<I>, Idx> | |
type Perms<Tuple extends readonly unknown[], Idx extends string> = | |
Idx extends Length<Tuple> ? Tuple : FixedIdx<Tuple, Idx, Idx> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment