Push
题目
在类型系统里实现通用的 Array.push
。
举例如下,
type Result = Push<[1, 2], '3'> // [1, 2, '3']
题目来源:https://tsch.js.org/3057/zh-CN
解答
type Push<T extends any[], U> = [...T, U];
使用extends any []
限制泛型T
为数组类型,
然后,使用扩展运算符展开T
,进行合并。
在类型系统里实现通用的 Array.push
。
举例如下,
type Result = Push<[1, 2], '3'> // [1, 2, '3']
题目来源:https://tsch.js.org/3057/zh-CN
type Push<T extends any[], U> = [...T, U];
使用extends any []
限制泛型T
为数组类型,
然后,使用扩展运算符展开T
,进行合并。