如何输入一个多维数组,其中每个数组具有不同的类型?

huangapple go评论92阅读模式
英文:

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[]] {}

huangapple
  • 本文由 发表于 2023年7月27日 16:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定