[Typescript] 55. Medium - Reverse

Implement the type version of Array.reverse

For example:

type a = Reverse<['a', 'b']> // ['b', 'a']
type b = Reverse<['a', 'b', 'c']> // ['c', 'b', 'a']

 

/* _____________ Your Code Here _____________ */

type Reverse<T> = T extends [...infer H, infer T] ? [T, ...Reverse<H>] : [];

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

type cases = [
  Expect<Equal<Reverse<[]>, []>>,
  Expect<Equal<Reverse<['a', 'b']>, ['b', 'a']>>,
  Expect<Equal<Reverse<['a', 'b', 'c']>, ['c', 'b', 'a']>>,
]

 

posted @ 2022-10-18 19:12  Zhentiw  阅读(8)  评论(0)    收藏  举报