Skip to main content

Omit

introduction#

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

ts
interface Todo {
title: string
description: string
completed: boolean
}
type TodoPreview = MyOmit<Todo, 'description' | 'title'>
const todo: TodoPreview = {
completed: false,
}
ts
interface Todo {
title: string
description: string
completed: boolean
}
type TodoPreview = MyOmit<Todo, 'description' | 'title'>
const todo: TodoPreview = {
completed: false,
}
View on GitHub

start point#

ts
/* _____________ Your Code Here _____________ */
type MyOmit<T, K> = any
 
/* _____________ Test Cases _____________ */
type cases = [
Expect<Equal<Expected1, MyOmit<Todo, 'description'>>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Expected2, MyOmit<Todo, 'description' | 'completed'>>>
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
interface Todo {
title: string
description: string
completed: boolean
}
 
interface Expected1 {
title: string
completed: boolean
}
 
interface Expected2 {
title: string
}
Try
ts
/* _____________ Your Code Here _____________ */
type MyOmit<T, K> = any
 
/* _____________ Test Cases _____________ */
type cases = [
Expect<Equal<Expected1, MyOmit<Todo, 'description'>>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Expected2, MyOmit<Todo, 'description' | 'completed'>>>
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
interface Todo {
title: string
description: string
completed: boolean
}
 
interface Expected1 {
title: string
completed: boolean
}
 
interface Expected2 {
title: string
}
Try
take the challenge

my solutions#

Spoiler warning // Click to reveal answer
ts
type MyOmit<T , K extends string | number | symbol> = {
[Key in Exclude<keyof T, K>]: T[Key]
}
Try
ts
type MyOmit<T , K extends string | number | symbol> = {
[Key in Exclude<keyof T, K>]: T[Key]
}
Try
view more solutions