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?

FFaFafy
  • 27
  • 5
  • 3
    No, you can't convert type into runtime value. Types are erasing during the compilation – captain-yossarian from Ukraine Dec 14 '21 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: https://stackoverflow.com/questions/60131681/make-sure-array-has-all-types-from-a-union – zecuria Dec 14 '21 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. – FFaFafy Dec 14 '21 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 – Guerric P Dec 14 '21 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. – FFaFafy Dec 15 '21 at 08:03

0 Answers0