Go Back

Refer: Medium - Omit

Describe

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

code snippetCopytypescript
interface Todo { title: string description: string completed: boolean } type TodoPreview = MyOmit<Todo, 'description' | 'title'> const todo: TodoPreview = { completed: false, }

Test Cases

code snippetCopytypescript
import { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<Expected1, MyOmit<Todo, 'description'>>>, Expect<Equal<Expected2, MyOmit<Todo, 'description' | 'completed'>>>, ] // @ts-expect-error type error = MyOmit<Todo, 'description' | 'invalid'> interface Todo { title: string description: string completed: boolean } interface Expected1 { title: string completed: boolean } interface Expected2 { title: string }

Solution

code snippetCopytypescript
type MyOmit<T, K> = { [P in keyof T as P extends K ? never : P]: T[P] }