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.
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 falseChange the following code to make the test cases pass (no type check errors).
type All<T extends unknown[], V> = T extends [infer First, ...infer Rest]
? Equal<First, V> extends true
? All<Rest, V>
: false
: trueHow it works:
T extends [infer First, ...infer Rest] recursively deconstructs the tuple, extracting the first element and the remaining elementsEqual<First, V> extends true uses the Equal utility to perform a strict type equality check between the current element and the target type V, which correctly handles edge cases like any, unknown, never, and union typesAll<Rest, V> recurses on the remaining elementsfalsetrue since all elements have been checkedThis 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.
Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.
Only 27 Spots left
Get 1 month early access
Pre-Launch discount