#527Medium

Append to object

Implement a Generic Type that can append a new field to any object type. Master mapped types in TypeScript in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement a Generic Type that can append a new field to any object type. For that you take a key and a value and append it to the object type.

Challenge Instructions: Append to object

Medium

Implement a type that adds a new field to the interface. The type takes the three arguments. The output should be an object with the new field.

For example

type Test = { id: '1' }
type Result = AppendToObject<Test, 'value', 4> // expected to be { id: '1', value: 4 }

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

Loading...

Video Walkthrough

Detailed Explanation

type AppendToObject<T, U extends PropertyKey, V> = {
  [Key in keyof T | U]: Key extends keyof T ? T[Key] : V
}

How it works:

This challenge helps you understand TypeScript's advanced type system and how to apply this concept 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