#8987Medium

Subsequence

Given an array of unique elements, return all possible subsequences. Learn union type manipulation in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement a Subsequence type that returns a union of all possible subsequences of a given tuple, preserving the original element order.

Challenge Instructions: Subsequence

Medium

Given an array of unique elements, return all possible subsequences.

A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements.

For example:

type A = Subsequence<[1, 2]> // [] | [1] | [2] | [1, 2]

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

Loading...

Detailed Explanation

type Subsequence<T extends any[]> = T extends [infer F, ...infer Rest]
  ? [F, ...Subsequence<Rest>] | Subsequence<Rest>
  : []

How it works:

This challenge helps you understand recursive type-level combinatorics and union type generation and how to apply these concepts in real-world scenarios.

This challenge is originally from here.

Share this challenge

Learn the Concepts

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