#949Medium

AnyOf

Implement Python liked `any` function in the type system. A type takes the Array and returns `true` if any element of the Array is true. If the Array is empty, return `false`. Learn array type operations in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement a Python-like AnyOf type that takes an array and returns true if any element is truthy, or false if the array is empty or all elements are falsy.

Challenge Instructions: AnyOf

Medium

Implement Python liked any function in the type system. A type takes the Array and returns true if any element of the Array is true. If the Array is empty, return false.

For example:

type Sample1 = AnyOf<[1, "", false, [], {}]> // expected to be true.
type Sample2 = AnyOf<[0, "", false, [], {}]> // expected to be false.

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

Loading...

Detailed Explanation

type Falsy = 0 | '' | false | [] | { [key: string]: never } | undefined | null
 
type AnyOf<T extends readonly any[]> = T extends [infer First, ...infer Rest]
  ? First extends Falsy
    ? AnyOf<Rest>
    : true
  : false

How it works:

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

This challenge is originally from here.

Share this challenge

Join early, learn faster.

Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.

No spam, unsubscribe at any time. We respect your privacy.

Limited Availability

Only 27 Spots left

Early Access

Get 1 month early access

>75% Off

Pre-Launch discount