1

I have union type:

type A = "a" | "b" | "c";

And I would like to make some type B which will force an array to have all values listed in type A:

const arr: B = ["a", "b", "c"];
Dawid Krajewski
  • 324
  • 2
  • 15
  • No, this seems to be not related to my case. I want to transform: `type A = "a" | "b" | "c"` to `type B = ["a", "b", "c"]` or to other type which will force an array to have all of the values from union - not necessarily in the same order – Dawid Krajewski Apr 19 '20 at 19:59
  • 1
    So you don't just want "some type B" that requires all members to be in the array (that's what the linked question, and the further linked question from that one, are about), you specifically want *this particular* type B? In that case you are trying to convert a union to a tuple, so your question is a duplicate of this one: [How to transform union type to tuple type](https://stackoverflow.com/q/55127004/12299000) – kaya3 Apr 19 '20 at 20:03
  • Also, I think it smells like a bad idea (as the main answer in the last duplicate suggestion). To avoid X/Y problem solving, I recommend that you actually share your underlying goal, maybe there is another way to achieve what you need to solve. – Pac0 Apr 19 '20 at 20:06
  • If you are defining type `A` yourself it might be better to write it the other way round: `const arr = ["a", "b", "c"] as const; type B = typeof arr; type A = B[number];` – Artyer Apr 19 '20 at 21:19

0 Answers0