具有不同类型的typescript多维数组

时间:2016-04-13 04:06:50

标签: arrays multidimensional-array types typescript angular

我有一个自定义对象声明(ThreadObj),我想创建一个THREADLISTS,持有多个Threadlist数组。所以

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

第一个dim是ThreadObj [],第二个是ThreadObj。

干杯

2 个答案:

答案 0 :(得分:2)

示例:

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

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

更多

一步到位:

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

答案 1 :(得分:2)

不会那样:

let arr:ThreadObj[][] = []