This solution is generated by ChatGPT, correctness is not guaranteed. You might also be able to find a solution fromcommunity posts.
AI solution for TypeScript puzzle on BFE.dev
6. implement Omit<T, K>
Here's one possible solution using mapped types and the keyof
operator:
type MyOmit<T, K extends keyof T> = { [P in Exclude<keyof T, K>]: T[P]}
Explanation:
- We use the mapped type syntax (
{ [P in Keys]: Type }
) to create a new type with the same keys asT
but with a subset of the original properties. - The
Exclude<T, U>
utility type is used to exclude the keys inK
from the original key set (which iskeyof T
). - The result is a type with all the properties from
T
except for the ones listed inK
.
Let's test it with the sample types:
type Foo = { a: string b: number c: boolean}type A = MyOmit<Foo, 'a' | 'b'> // { c: boolean }type B = MyOmit<Foo, 'c'> // { a: string; b: number; }type C = MyOmit<Foo, 'c' | 'd'> // { a: string; b: number; }
As expected, we get the desired results.