Typescript 合并元组

标签 typescript typescript-generics

如何将元组合并在一起?我需要在另一个元组的末尾添加一个元组,如下所示。

type MergeTuple<A extends any[], B extends any[]> = [...A, ...B];

唯一有效的是:

type MergeTuple<A extends any[], B extends any[]> = [A[0], B[0]];

但这适用于项目的静态计数

MergeTuple<['a'], ['b']> // is ['a', 'b']

不适合这个

MergeTuple<['a', 'b'], ['c']> // is ['a', 'c'] expected ['a', 'b', 'c']

最佳答案

目前,在 TS 中连接元组只能通过非常丑陋的解决方法来实现。

Here is how one library does it

你不想自己这么做。问问自己是否真的需要它,如果答案是肯定的,那就帮自己一个忙,使用像 typescript-tuple 这样的类型库。

关于Typescript 合并元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59625905/

相关文章:

typescript - 如何将方法的泛型类型限制为 typescript 中的对象?

typescript - typescript 中的相交键

javascript - 在 Angular 6 component.html 中使用 script 标签

node.js - 刷新 token API 错误 "secretOrPrivateKey must have a value"

typescript - 在发布版本中剥离 Typescript 装饰器

typescript - 这个 `Type ' any[ ]' is not assignable to type ' [number]` TypeScript 错误的原因是什么?

typescript - 输入 Object.fromEntries(new FormData(form))

typescript - 如何获取嵌套对象的类型推断?

node.js - 扩展 Express 应用程序接口(interface)

javascript - 使用 Typescript 接口(interface)作为变量