typescript - 在 typescript 中创建元组数组

标签 typescript tuples

创建通用对类型(最简单的元组)很容易,即

type Pair<A, B> = [A, B]


问题是如何创建一个表示此类通用对和数组的类型。

唯一的要求是数组中的元素必须成对。第一个元素的类型和第二个元素的类型应该是多态的,any不会进行剪切,否则这将是令人满意的:

type Pair = [any, any]
type Pairs = Pair[]

最佳答案

我感觉好像在这里错过了细微差别。。但是请相信这是您要的:

type Pair<T,K> = [T,K];
type Pairs<T,K> = Pair<T,K>[];

  const apple: Pair<number,number> = [2,3];
  const orange: Pair<number,number> = [3,4];
  const food: Pairs<number, number> = [apple, orange];

关于typescript - 在 typescript 中创建元组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55944130/

相关文章:

module - typescript 0.9.* VisualStudio TS5037 : Cannot compile external modules unless the '--module' flag is provided

typescript - 在 TypeScript 中重新导出为默认值

swift - swift 中不同大小和类型的元组列表

python - 为什么元组在评估生成器自身时比列表慢?

python - 从由嵌套元组键控的 python 字典重建向量场

arrays - julia 函数从元组数组中选择一个元组

javascript - 如何在 Typescript 中推送数组数组中的元素

angular - 有条件地更改类 Ionic2

TypeScript 和 Koa 2 : async/await issue with Global Error Handler

c++ - 冲突的不匹配标签与标准库一起编译,但不以其他方式编译