Pop
题目
实现一个通用Pop<T>
,它接受一个数组T
并返回一个没有最后一个元素的数组。
例如
type arr1 = ['a', 'b', 'c', 'd']
type arr2 = [3, 2, 1]
type re1 = Pop<arr1> // expected to be ['a', 'b', 'c']
type re2 = Pop<arr2> // expected to be [3, 2]
题目来源:https://tsch.js.org/16/zh-CN
解答
type Pop<T extends any[]> = T extends [...infer K, infer U] ? K : never;
使用infer
进行指代,配合解构,即可解开此题。