#29785Medium

Deep Omit

Implement a type`DeepOmit`, Like Utility types [Omit](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys), A type takes two arguments. Master advanced TypeScript type manipulation in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement DeepOmit<T, S> which works like the built-in Omit utility type but supports dot-separated paths to omit deeply nested properties from an object type.

Challenge Instructions: Deep Omit

Medium

Implement a typeDeepOmit, Like Utility types Omit, A type takes two arguments.

For example:

type obj = {
person: {
name: string;
age: {
value: number
}
}
}
 
type test1 = DeepOmit<obj, 'person'>    // {}
type test2 = DeepOmit<obj, 'person.name'> // { person: { age: { value: number } } }
type test3 = DeepOmit<obj, 'name'> // { person: { name: string; age: { value: number } } }
type test4 = DeepOmit<obj, 'person.age.value'> // { person: { name: string; age: {} } }

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

ChallengeSolution
type obj = {
  person: {
    name: string
    age: {
      value: number
    }
  }
}

type cases = [
  Expect<Equal<DeepOmit<obj, 'person'>, {}>>,
  Expect<
    Equal<DeepOmit<obj, 'person.name'>, { person: { age: { value: number } } }>
  >,
  Expect<Equal<DeepOmit<obj, 'name'>, obj>>,
  Expect<
    Equal<
      DeepOmit<obj, 'person.age.value'>,
      { person: { name: string; age: {} } }
    >
  >,
]

Pro Challenge

Unlock 102+ medium, hard, and extreme challenges to master advanced TypeScript.

One-time payment. Lifetime access.

Detailed Explanation

type DeepOmit<T, S extends string> = {
  [K in keyof T as K extends (S extends `${infer First}.${string}` ? First : S) ? never : K]:
    S extends `${infer First}.${infer Rest}`
      ? K extends First
        ? DeepOmit<T[K], Rest>
        : T[K]
      : T[K]
}

How it works:

This challenge helps you understand recursive mapped types with key filtering and dot-path string parsing, and how to apply these concepts in real-world scenarios.

This challenge is originally from here.

Share this challenge

Learn the Concepts