1

I am trying to figure out how to create an Array out of a union type that I have created earlier.

Example:

type Temperature = 'cold' | 'warm' | 'hot';

// The end result of my array should look like this:
const ArrayOfTemperatures = ['cold', 'warm', 'hot'];

As seen in the example above, I would like to convert a union type into an array of those types, without allowing duplication if possible.

  • 1
    To be clear, your code is currently showing something that's impossible. You can't turn a union type (which is a purely compile-time constant) into a runtime value (an array with those strings in it). (You *can* do the opposite if the runtime value is constant, and thus known at compile time.) Are you really trying to do that, or are you trying to create a tuple type (not a runtime value) as in @caTS's link? – T.J. Crowder Oct 13 '22 at 12:56
  • 1
    (Doing the opposite, in case it helps: https://www.typescriptlang.org/play?#code/MYewdgzgLgBAggJwQQwJ4HkBmAVApgWwAdcUoBXBXCGAXhgG0AiUAGwBNGAaGRgd2QT4uPABYgojALoxk1UJCgBuAFBRUxGHiIlk5SrRhriITPCRosW4qQpV6YMvgBGJSSoD07mN4B6AfiA) – T.J. Crowder Oct 13 '22 at 13:04

0 Answers0