#4260Medium

AllCombinations

Implement type ```AllCombinations<S>``` that return all combinations of strings which use characters from ```S``` at most once. Master advanced TypeScript type manipulation in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement AllCombinations<S> that returns all possible permutations and combinations of characters from the string S, using each character at most once.

Challenge Instructions: AllCombinations

Medium

Implement type AllCombinations<S> that return all combinations of strings which use characters from S at most once.

For example:

type AllCombinations_ABC = AllCombinations<'ABC'>;
// should be '' | 'A' | 'B' | 'C' | 'AB' | 'AC' | 'BA' | 'BC' | 'CA' | 'CB' | 'ABC' | 'ACB' | 'BAC' | 'BCA' | 'CAB' | 'CBA'

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

Loading...

Detailed Explanation

type StringToUnion<S extends string> = S extends `${infer First}${infer Rest}`
  ? First | StringToUnion<Rest>
  : never
 
type Combinations<U extends string, S extends string = U> =
  S extends S
    ? '' | `${S}${Combinations<Exclude<U, S>>}`
    : never
 
type AllCombinations<S extends string> = Combinations<StringToUnion<S>>

How it works:

This challenge helps you understand distributive conditional types, recursive type generation, and template literal types, 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