1

Is it possible to convert union type in Typescript to array type which requires to have all of the values defined in union type?

type Colors = "primary" | "secondary" | "error";

I would like it to convert to:

type ColorsArray = ["primary", "secondary", "error"];
Dawid Krajewski
  • 324
  • 2
  • 15
  • 2
    Not possible, union order is not defined it may be represented internally in a different order to the one you specified and this might even differ between build. There are hacks to do something like this don't use them. There is a version that generates a union of tuples with all possible permutations, do not use it for any union larger than 8 (with 9 you would already get a union of 9! = 362880 constituents and this will kill your compilation time). try to find another alternative design. – Titian Cernicova-Dragomir Jul 16 '19 at 10:54

0 Answers0