3

I have a type declaration something like

type myType = "TYPE_1" | "TYPE_2" | "TYPE_3"

and I want to convert that to a string list,

const myTypeArr = ["TYPE_1", "TYPE_2", "TYPE_3"]

so I could iterate over it. Is it possible?

5
  • 3
    No, you can't convert type into runtime value. Types are erasing during the compilation Commented Dec 14, 2021 at 11:59
  • 1
    While you can't convert a type into an array. There are ways to create an array and confirm if all the possible values of myType exist in it. By doing: stackoverflow.com/questions/60131681/… Commented Dec 14, 2021 at 12:09
  • The reason why I need this is, there is an automated tool that extracts possible types from a data object from our API as a ts interface, and I would like to create a generic filtering tool for a table in the UI by mapping the values to checkbox. I am open to ideas. Commented Dec 14, 2021 at 13:34
  • Your automated tool should generate JavaScript code (objects, arrays, ...) instead of TypeScript interfaces. Note that this JavaScript code could be typed with TypeScript, the important thing is that they should contain runtime constructs and not simply TypeScript types Commented Dec 14, 2021 at 19:23
  • Thank you, however I have no authority over that utility. I guess I have to enforce all types exist in an array with what @zecuria had suggested, and hope that I did not hurt feelings of future developers. Commented Dec 15, 2021 at 8:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.