Merging types instead of using an intersection when using generics

I have a generic type like this one :
generic('T', 'M')([ 'T', '&', { metadata: { id: 'M' }} ])
generic('T', 'M')([ 'T', '&', { metadata: { id: 'M' }} ])
If the first argument already contains the property metadata, I would like this property to be dropped and replaced by the one from my generic type, instead of having an intersection, like in my example. Is it possible to do something like that ?
5 Replies
Dimava
Dimava4mo ago
Try the Omit generic
JesusTheHun
JesusTheHunOP4mo ago
It works nicely, but if there is any nesting to deal with, it will become impractical :/ I'm gonna open a feature request
Dimava
Dimava4mo ago
Sounds difficult You probably will need mapProperties and a Hkt generic for that
JesusTheHun
JesusTheHunOP4mo ago
what is a HKT ?
Dimava
Dimava4mo ago
I you need a generic that it not a definition you need
const ReadonlyArray = generic("T")(({ T }) => {
return T.array()
}, class extends Hkt {
body: ReadonlyArray< /* T */ this[0] >
})
const ReadonlyArray = generic("T")(({ T }) => {
return T.array()
}, class extends Hkt {
body: ReadonlyArray< /* T */ this[0] >
})

Did you find this page helpful?