BFE.dev solution for TypeScript puzzle
6. implement Omit<T, K>
author by @jichen257
Note that K extends keyof any
, which means that K binds to string | number | symbol
, i.e. to all desirable types of keys in the object
// solution 1
type MyOmit<T, K extends keyof any> = {
[P in keyof T as P extends K ? never : P]: T[P]
}
Of course, where P in keyof T as P extends K ? never : P
can also be abbreviated to P in Exclude<keyof T, K>
, which both mean "returns P by removing from T the union members that are assignable to K.", and the usage of Exclude
is not repeated here (there is a corresponding topic later)
// solution 2
type MyOmit<T, K extends keyof any> = {
[P in Exclude<keyof T, K>]: T[P]
}
You might also be able to find a solution fromcommunity posts or fromAI solution.