Created
November 6, 2024 19:17
-
-
Save aleclarson/ba1b326aefc24091cdc5e8b35af1790e to your computer and use it in GitHub Desktop.
Get the permutations of a union type
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
export type Permutations<T, TInitial = T> = T extends infer TSingle | |
? | |
| [TSingle] | |
| (Permutations<Exclude<TInitial, TSingle>> extends infer TPermutation | |
? TPermutation extends any[] | |
? [T | TPermutation[number]] | |
: never | |
: never) | |
: never | |
export type RecordPermutations< | |
K extends PropertyKey, | |
V, | |
> = Permutations<K> extends infer TPermutation | |
? TPermutation extends [infer K extends PropertyKey] | |
? Record<K, V> | |
: never | |
: never |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment