I am inquiring about the process of converting an array of string variables to TypeScript types.
It is worth noting that there are two existing SO answers addressing this issue, but they involve passing strings hardcoded to maintain their original values.
- How to convert array of strings to typescript types?
- Convert array of strings to TypeScript type
Both solutions pass an array directly to preserve string values as literals. However, when passing an array as a variable, it fails to function correctly.
Using inline values works.
https://codesandbox.io/s/30y547yl91 https://i.sstatic.net/g9HcY.png
However, using variable values does not work.
https://codesandbox.io/s/qrrq24k7q https://i.sstatic.net/JLuja.png
Question
So, how can one create a type that restricts a list of allowable string values in domElements
as a variable to asLiterals
?