#3•Medium

Omit

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.

Challenge Instructions: Omit

Medium

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).

ChallengeSolution
// Video Solution: https://youtube.com/watch?v=ojagklq8aas

/* _____________ Your Code Here _____________ */

type MyOmit<T, K> = any

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '../helpers'

type cases = [
  Expect<Equal<Expected1, MyOmit<Todo, 'description'>>>,
  Expect<Equal<Expected2, MyOmit<Todo, 'description' | 'completed'>>>,
  Expect<Equal<Expected3, MyOmit<Todo1, 'description' | 'completed'>>>,
]

// @ts-expect-error
type error = MyOmit<Todo, 'description' | 'invalid'>

interface Todo {
  title: string
  description: string
  completed: boolean
}

Pro Challenge

Get access to all 200+ challenges, including every medium, hard, and extreme one.

One-time payment, lifetime access. 30-day money-back guarantee.

Video Walkthrough

Detailed Explanation

To omit properties from a type, we combine Pick and Exclude:

[object Object]

How it works:

To 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:

This challenge helps you understand union type manipulation and how to apply this concept in real-world scenarios.

This challenge is originally from here.

Share this challenge

Related Challenges

Learn the Concepts

Become a TypeScript Pro

Track your progress through 200+ hands-on challenges. Free, sign in with GitHub.

Or start solving right away: explore all TypeScript challenges