0

Let's say I have a type, for example:

type Contact = "email" | "phone"

Is there a way to convert the type Contact to a list of strings ["email", "phone"], so I could use that list at runtime, as an ordinary javascript value?

user246185
  • 136
  • 2
  • 6
  • 1
    Does this answer your question? [TypeScript array to string literal type](https://stackoverflow.com/questions/44497388/typescript-array-to-string-literal-type) – falinsky Jul 17 '20 at 22:33

1 Answers1

1

You can't go from type to list, but you can go the other way around. You could do something like this:

const CONTACTS = ['email', 'phone'] as const
type Contact = typeof CONTACTS[number]

Then you can manage that list in only one place

delashum
  • 801
  • 7
  • 16