#3188Medium

Tuple to Nested Object

Given a tuple type ```T``` that only contains string type, and a type ```U```, build an object recursively. Learn tuple manipulation in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement a TupleToNestedObject type that takes a tuple of string keys and a value type, then builds a deeply nested object type where each key wraps the next level.

Challenge Instructions: Tuple to Nested Object

Medium

Given a tuple type T that only contains string type, and a type U, build an object recursively.

type a = TupleToNestedObject<['a'], string> // {a: string}
type b = TupleToNestedObject<['a', 'b'], number> // {a: {b: number}}
type c = TupleToNestedObject<[], boolean> // boolean. if the tuple is empty, just return the U type

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

Loading...

Detailed Explanation

type TupleToNestedObject<T extends string[], U> =
  T extends [infer First extends string, ...infer Rest extends string[]]
    ? { [K in First]: TupleToNestedObject<Rest, U> }
    : U;

How it works:

This challenge helps you understand recursive type construction and tuple-to-object transformation, 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