zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[Typescript] 53. Medium - Shift

typescript Medium 53 shift
2023-09-14 09:00:45 时间

Implement the type version of Array.shift

For example

type Result = Shift<[3, 2, 1]> // [2, 1]

 

/* _____________ Your Code Here _____________ */

type Shift<T extends any[], ACC = T> = T extends [infer REMOVED, ...(infer REST)] ? REST: [];


/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  // @ts-expect-error
  Shift<unknown>,
  Expect<Equal<Shift<[]>, []>>,
  Expect<Equal<Shift<[1]>, []>>,
  Expect<Equal<Shift<[3, 2, 1]>, [2, 1]>>,
  Expect<Equal<Shift<['a', 'b', 'c', 'd']>, ['b', 'c', 'd']>>,
]