You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using Merge with an indexed type on either left or right hand all the well-known properties are lost. This is because keyof of an indexed type always returns 'string | number', because every key is possible in that object.
Example:
typeExample={foo: string,bar: symbol,[key: string]: unknown,}typeFoo={bar: Date,}/* We would expect something like: { foo: string, bar: Date, [key: string]: unknown, } but it actually returns: { [key: string]: unknown, [key: number]: unknown, }*/typeresult=Merge<Example,Foo>
And Omit indeed loses the well-known keys in the same way:
/* Returns: { [key: string]: unknown, [key: number]: unknown, }*/typeomit=Omit<Example,'foo'>/* Our current implementation of Except on the other hand works fine with indexed types This returns: { bar: Date, [key: string]: unknown, }*/typeexcept=Except<Example,'foo'>
What I propose is to either change Merge to support indexed types and return what I'm expecting on the example (similar to except), which could be a breaking change for people expecting to lose typing on these cases (but I doubt is a useful behavior), or we can create a new MergeWellKnownKeys that treats indexed types. What do you guys think?
I can work on a PR as soon as we decide on an implementation. It should be easy because we already have an internal type doing the same thing on our company's project
Upvote & Fund
We're using Polar.sh so you can upvote and help fund this issue.
The funding will be given to active contributors.
Thank you in advance for helping prioritize & fund our backlog.
The text was updated successfully, but these errors were encountered:
When using
Merge
with an indexed type on either left or right hand all the well-known properties are lost. This is becausekeyof
of an indexed type always returns 'string | number', because every key is possible in that object.Example:
This is a well-known caveat with Typescript that was discussed in a few issues (microsoft/TypeScript#45367, microsoft/TypeScript#31153), the behavior is actually intended.
And
Omit
indeed loses the well-known keys in the same way:What I propose is to either change
Merge
to support indexed types and return what I'm expecting on the example (similar to except), which could be a breaking change for people expecting to lose typing on these cases (but I doubt is a useful behavior), or we can create a newMergeWellKnownKeys
that treats indexed types. What do you guys think?I can work on a PR as soon as we decide on an implementation. It should be easy because we already have an internal type doing the same thing on our company's project
Upvote & Fund
The text was updated successfully, but these errors were encountered: