Home > Software engineering >  How to create a Typescript declaration from object to an array of arrays
How to create a Typescript declaration from object to an array of arrays

Time:08-11

I have the following object:

myObj = [ [ x, y, z ], [ k, l ], [ i ] ]

The values of x, y, k and all others can be any value from this other object:

options = { A: 'red', B: 'green', C: 'blue' }

How to declare the type of myObj? So I can get this:

type MyType = ???
myObj: MyType = [ [ ...

Having hard time to figuring this out.

Appreciate your attention!!!

Edit: Codepen

CodePudding user response:

type Color = ('red'|'green'|'blue')
type MyType = [[Color,Color,Color],[Color,Color],[Color]]
const myObj: MyType = [['red','red','red'],['red','red'],['red']]

CodePudding user response:

type MyType = ('red'|'green'|'blue')[][]
  • Related