[Typescript] 53. Medium - Shift

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']>>,
]

 

posted @ 2022-10-17 14:41  Zhentiw  阅读(16)  评论(0编辑  收藏  举报