Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Created November 6, 2024 19:17
Show Gist options
  • Save aleclarson/ba1b326aefc24091cdc5e8b35af1790e to your computer and use it in GitHub Desktop.
Save aleclarson/ba1b326aefc24091cdc5e8b35af1790e to your computer and use it in GitHub Desktop.
Get the permutations of a union type
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