[Typescript] 133. Medium - All

Returns true if all elements of the list are equal to the second parameter passed in, false if there are any mismatches.

For example

type Test1 = [1, 1, 1]
type Test2 = [1, 1, 2]

type Todo = All<Test1, 1> // should be same as true
type Todo2 = All<Test2, 1> // should be same as false

 

/* _____________ Your Code Here _____________ */

type All<T extends any[], U extends any = T[0]> = T extends [infer H, ...infer RT]
  ? Equal<H, U> extends true 
    ? All<RT, U>
    : false
  : true


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

type cases = [
  Expect<Equal<All<[1, 1, 1], 1>, true>>,
  Expect<Equal<All<[1, 1, 2], 1>, false>>,
  Expect<Equal<All<["1", "1", "1"], "1">, true>>,
  Expect<Equal<All<["1", "1", "1"], 1>, false>>,
  Expect<Equal<All<[number, number, number], number>, true>>,
  Expect<Equal<All<[number, number, string], number>, false>>,
  Expect<Equal<All<[null, null, null], null>, true>>,
  Expect<Equal<All<[[1], [1], [1]], [1]>, true>>,
  Expect<Equal<All<[{}, {}, {}], {}>, true>>,
];

 

posted @ 2022-12-11 00:03  Zhentiw  阅读(14)  评论(0编辑  收藏  举报