Unshift
题目
实现类型版本的 Array.unshift
。
举例,
type Result = Unshift<[1, 2], 0> // [0, 1, 2,]
题目来源:https://tsch.js.org/3060/zh-CN
解答
type Unshift<T extends any[], U> = [U, ...T];
使用extends any []
限制泛型T
为数组类型,
然后,使用扩展运算符展开T
,进行合并。
实现类型版本的 Array.unshift
。
举例,
type Result = Unshift<[1, 2], 0> // [0, 1, 2,]
题目来源:https://tsch.js.org/3060/zh-CN
type Unshift<T extends any[], U> = [U, ...T];
使用extends any []
限制泛型T
为数组类型,
然后,使用扩展运算符展开T
,进行合并。