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?

3
  • 2
    "For some reason"... you don't have any sort of iteration so why would you assume that anything automatic would occur? Commented Jul 18, 2022 at 23:37
  • 1
    Does this answer your question? How to iterate a string literal type in typescript Commented Jul 18, 2022 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! Commented Jul 20, 2022 at 21:56

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.