英文:
How to type a multidimensional array whose each array has different type?
问题
对于每个元素具有相同类型的多维数组,声明起来很简单:string[][]
。
但是如果每个数组的类型不同,我该怎么做呢?
function create3DArray(arg): // <----???
{ firstArray: string[] = []
secondArray: number[] = []
thirdArray: CustomType[] = []
return [firstArray, secondArray, thirdArray]
}
我已经查看了typescript multidimensional array with different types,但我仍然不确定该如何做。
英文:
For a multidimensional array whose every element has the same type, it's simple to declare: string[][]
.
However if each array is different, then how should I do?
function create3DArray(arg): // <----???
{ firstArray: string[] = []
secondArray: number[] = []
thirdArray: CustomType[] = []
return [firstArray, secondArray, thirdArray]
}
I have looked at typescript multidimensional array with different types but I'm still not sure how to do it.
答案1
得分: 1
你正在寻找一个元组。元组是一个有限长度的数组,在你的情况下,长度为三:
function create2DArray(arg): [string[], number[], CustomType[]] {}
英文:
You are looking for a tuple. Tuple is an array with finite length, in your case, it is three:
function create2DArray(arg):[string[], number[], CustomType[]] {}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论