DEV Community

Acid Coder
Acid Coder

Posted on • Edited on

Typescript Turn Tuple Type to Array

here is how you do it

type TupleToArray<
  T extends unknown[],
  Acc = never
> = T extends [infer H, ...infer Rest]
  ? Rest extends []
    ? (H | Acc)[]
    : TupleToArray<Rest, Acc | H>
  : "impossible route"

type A = TupleToArray<[number, string, boolean]> // (string | number | boolean)[]
Enter fullscreen mode Exit fullscreen mode

playground

joke, I was messing, here is the easy way

type B = [number, string, boolean][number][] // (string | number | boolean)[]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)