I have implemented the built in generic Omit which relies on the generic Exclude.
type MyExclude<C, E extends C> = C extends E ? never : C
type MyOmit<T, K extends keyof T> = {
[p in MyExclude<keyof T, K>]: T[p] //Returns object with unwanted properties omitted
}
At the moment I cannot successfully implement Omit without first declaring an Exclude generic and consuming it within Omit. An attempt to do so throws an error.
type MyOmitInLine<T, K extends keyof T> = {
[p in (keyof T extends K ? never : keyof T)]: T[p] //Returns complete object
}
However, I would like to implement Omit without consuming on any other generics, and I believe the key to doing this is by:
- declaring a type variable inline
- back referencing to the declared type variable
Like so:
type MyOmitWish<T, K extends keyof T> = {
[p in ((keyof T as C) extends K ? never : C)]: T[p] //What I want -- is this possible?
}
Please is this possible?
Link to use this code in the TypeScript playground
UPDATE
I found something which is exactly what I was hoping for but I don't understand it just yet:
type MyOmit<T, U extends keyof T> = {
[P in keyof T as P extends U ? never : P]: T[P] //works without erros
}