0

Here is the code that i have:

type keyList = 'a' | 'b' | 'c'
type valueList = 'x' | 'y' | 'z'

I am trying to create an array of objects which such that -

const arrayOfObjects:{key:keyList, value:valueList}[] = [
 {
   key:'a',
   value:'x'
 },...

And so the keys and values are combinations of values from keyList and valueList - where all items from the keyList appear once in the array and take any value from valueList.

For some reason, my 'arrayOfObjects' object does not automatically populate all values from keyList at least once. I have about 100 keys and it is very tedious to create individual objects for that. Is there any way I can get it automatically populated?

Phil
  • 157,677
  • 23
  • 242
  • 245
jdiablo
  • 17
  • 6
  • 2
    _"For some reason"_... you don't have any sort of iteration so why would you assume that anything automatic would occur? – Phil Jul 18 '22 at 23:37
  • 1
    Does this answer your question? [How to iterate a string literal type in typescript](https://stackoverflow.com/questions/43067354/how-to-iterate-a-string-literal-type-in-typescript) – Phil Jul 18 '22 at 23:39
  • I assumed it because I had a similar structure in my code and it would throw an error if I missed out a value. And I'm sorry for the wrong wording - by automatic i meant VSCode would detect and suggest a fix. The link helps - thanks! – jdiablo Jul 20 '22 at 21:56

0 Answers0