5

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

Han Che
  • 8,239
  • 19
  • 70
  • 116
  • Possible duplicate of [Typescript - multidimensional array initialization](http://stackoverflow.com/questions/30144580/typescript-multidimensional-array-initialization) – David Sherret Apr 13 '16 at 04:12
  • 1
    I've seen that thread, which unfortunately does not explain the typing question – Han Che Apr 13 '16 at 05:12
  • I believe it does—`ThreadObj[][]`. If that's not what you mean then can you provide an example of `THREADLISTS` use? Maybe we're misunderstanding. – David Sherret Apr 13 '16 at 13:52

3 Answers3

2

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'
        }
    ]
]
basarat
  • 261,912
  • 58
  • 460
  • 511
2

Wouldn't that just be:

let arr:ThreadObj[][] = []
ArcSine
  • 648
  • 6
  • 14
0

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"]];