Permutation
Challenge
Implement permutation type that transforms union types into the array that includes permutations of unions.
type perm = Permutation<"A" | "B" | "C">; // ['A', 'B', 'C'] | ['A', 'C', 'B'] | ['B', 'A', 'C'] | ['B', 'C', 'A'] | ['C', 'A', 'B'] | ['C', 'B', 'A']
Solution
Um das distribute Verhalten zu verhindern, packt man die Typen in einen Array:
type ToArrayNonDist<Type> = [Type] extends [any] ? Type[] : never;
type Permutation<T, U = T> = [T] extends [never]
? []
: T extends U
? [T, ...Permutation<Exclude<U, T>>]
: never;