#18142Medium

All

Returns true if all elements of the list are equal to the second parameter passed in, false if there are any mismatches. Learn array type operations in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement All<T, V> which returns true if all elements of the tuple T are exactly equal to the type V, and false otherwise.

Challenge Instructions: All

Medium

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

Change the following code to make the test cases pass (no type check errors).

ChallengeSolution
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>>,
  Expect<Equal<All<[never], never>, true>>,
  Expect<Equal<All<[any], any>, true>>,
  Expect<Equal<All<[unknown], unknown>, tr

Pro Challenge

Unlock 150+ medium, hard, and extreme challenges to master advanced TypeScript.

Monthly subscription. Cancel anytime.

Detailed Explanation

type All<T extends unknown[], V> = T extends [infer First, ...infer Rest]
  ? Equal<First, V> extends true
    ? All<Rest, V>
    : false
  : true

How it works:

This challenge helps you understand recursive tuple traversal and strict type equality checking, and how to apply these concepts in real-world scenarios.

This challenge is originally from here.

Share this challenge

Become a TypeScript Pro

Track your progress through 100+ hands-on challenges. Free, sign in with GitHub.

Or start solving right away: explore all TypeScript challenges