Type alias UnionToTuple<T, L, N>

UnionToTuple<T, L, N>: N extends true
    ? []
    : [...UnionToTuple<Exclude<T, L>>, L]

UnionToTuple<T> converts a union type T into a tuple type. This type is useful for scenarios where you need to work with the individual members of a union as an ordered list.

Type Parameters

  • T

    The union type to convert into a tuple.

  • L = LastOf<T>

    The last member of the union, used for recursive extraction.

  • N = [T] extends [never]
        ? true
        : false

    A boolean that checks if the union is empty.

Example

type TestUnion = 'a' | 'b' | 'c';
type ResultTuple = UnionToTuple<TestUnion>; // Result: ['a', 'b', 'c']