0

I want to turn this:

type UnionType = Variant1 | Variant2

into this:

type ResultingType = [UnionType, UnionType]

If the union has 3 member types, the tuple should have 3 elements.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Daniel Birowsky Popeski
  • 8,752
  • 12
  • 60
  • 125
  • Does this answer your question? [How to transform union type to tuple type](https://stackoverflow.com/questions/55127004/how-to-transform-union-type-to-tuple-type) – Terry Apr 15 '20 at 23:25
  • 1
    @Terry no. The ordering is important in that one. In mine, it has to be arbitrary. – Daniel Birowsky Popeski Apr 15 '20 at 23:38

1 Answers1

1

Do you mean "permutation"?

type UnionType = 'a' | 'b'

type UnionPermutation<U, K = U> =
  [U] extends [never]
  ? []
  : U extends any
  ? [U, ...UnionPermutation<Exclude<K, U>>]
  : never

// ['a', 'b'] | ['b', 'a']
type test = UnionPermutation<UnionType>

TypeScript Playground