Implement the built-in `Omit<T, K>` generic without using it. Learn union type manipulation in this medium-level challenge on TypeScriptPro.
In this medium-level challenge, you'll implement the built-in Omit<t, k> generic without using it.
You'll learn about union type manipulation, essential skills for advanced TypeScript development and type-level programming.
Implement the built-in Omit<T, K> generic without using it.
Constructs a type by picking all properties from T and then removing K
For example
interface Todo {
title: string
description: string
completed: boolean
}
type TodoPreview = MyOmit<Todo, 'description' | 'title'>
const todo: TodoPreview = {
completed: false,
}Change the following code to make the test cases pass (no type check errors).
To omit properties from a type, we combine Pick and Exclude:
type MyOmit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>How it works:
Exclude<keyof T, K> gets all keys of T except those in KPick creates a new type with only those remaining keysTo make it work without any built-in utility types, we need to use the keyof operator to get the keys of the type T and the Exclude operator to exclude the keys in K.
type MyOmit<T, K extends keyof T> = {
[P in keyof T as P extends K ? never : P]: T[P]
}How it works:
[P in keyof T as P extends K ? never : P] iterates over each key in TP extends K ? never : P checks if the key is in KT[P] gets the type of property P from the original type T, but only for the keys that are NOT in KThis challenge helps you understand union type manipulation and how to apply this concept in real-world scenarios.
This challenge is originally from here.
Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.
Only 27 Spots left
Get 1 month early access
Pre-Launch discount