【问题标题】:TypeScript tuple with array element带有数组元素的 TypeScript 元组
【发布时间】:2019-08-22 06:51:10
【问题描述】:

TypeScript 新手在这里。我做了一些快速搜索,但无法找到答案。问题是:元组数组的正确定义是什么,其中第一个元组元素是字符串,下一个元素是数字数组?我试过这个:

type labeledValuesType = [string, number[]]

 constructor(headings: string[], rows: Array<labeledValuesType>) {...

但在 VS Code 中,当我将鼠标悬停在传递给构造函数调用的第二个参数(行)上时,我看到:

const rows: (string | number[])[][]

它看起来像字符串和数字[] 的并集的二维数组。

【问题讨论】:

  • 你能提供一个真实的minimal reproducible example 来证明这个问题吗?在像the TypeScript Playground 这样的独立环境中重现问题是了解我们都在同一页面上的好方法。就目前而言,代码中rows 的类型在我的IDE 中显示为(parameter) rows: [string, number[]][]。也许您正在查看问题中未包含的某些变量的类型?

标签: typescript typescript-typings


【解决方案1】:

元组数组,有

  • 一个字符串作为第一个元素,
  • 作为第二个元素的数字数组

如果我对您搜索的内容没有错:

type MyTuple = [string, number[]];

const myArray: MyTuple[] = [
    ['str', [1, 2, 3]]
];

所以你的代码看起来没问题,你的问题涉及更多的 VS 代码。

【讨论】:

    猜你喜欢
    • 2021-09-29
    • 1970-01-01
    • 2020-06-03
    • 2019-07-12
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 2020-10-06
    相关资源
    最近更新 更多