排除最后一项
Less than 1 minuteTC-Medium
排除最后一项
题目
实现一个泛型Pop<T>,它接受一个数组T,并返回一个由数组T的前 N-1 项(N 为数组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]额外:同样,您也可以实现Shift,Push和Unshift吗?
解答
本题与0015 - 最后一个元素类似,我们可以使用infer来解决。
此处不做解析。
type Pop<T extends readonly any[]> = T extends [...infer Front, infer _] ? Front : []