Han Che
Han Che

Reputation: 8519

typescript multidimensional array with different types

I have an custom object declared (ThreadObj) and I want to create a THREADLISTS, holding multiple arrays of Threadlist. So

Threadlist:ThreadObj[]=[];
THREADLISTS:[ThreadObj[]][ThreadObj]=[][]; //how to type and init?

The first dim is of ThreadObj[] and the second is of ThreadObj.

Cheers

Upvotes: 5

Views: 14009

Answers (3)

Bimal Thapa Magar
Bimal Thapa Magar

Reputation: 1

For multi-dimensional array in typescript, you can simply declare and define the variable as

let multiArr:(string|number)[][] = [["Ram","Shyam",1,2,3,"Hari"]];

Upvotes: 0

ArcSine
ArcSine

Reputation: 644

Wouldn't that just be:

let arr:ThreadObj[][] = []

Upvotes: 2

basarat
basarat

Reputation: 276199

Example :

type ThreadObj = {foo:string}
type ThreadList = ThreadObj[]; 
type ThreadListList = ThreadList[];

const obj: ThreadObj = {
    foo: '123'
}
const singleDim: ThreadList = [
    obj
]
const multiDim: ThreadListList = [
    singleDim,
    singleDim
]

More

All in one step:

const allInOneStep: {foo:string}[][] = [
    [
        {
            foo: 'hello'
        },
        {
            foo: 'is it me'
        }
    ],
    [ 
        {
            foo: 'you are looking for'
        }
    ]
]

Upvotes: 3

Related Questions