Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Introduce
PickType
#92base: next
Are you sure you want to change the base?
feat: Introduce
PickType
#92Changes from 4 commits
cbd593a
915ffb0
1661465
e571626
0342943
e88944b
5e11e9b
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method feels kind of familiar, doesn't it? This is currently great to prove the concept. One of the things you had to change here compared to the implementation in
InterfaceType
is that "partial" is now an aspect of a single property, instead of the entire type.I think we should consider refactoring
InterfaceType
to support that as well. All the ingredients are already in place. This could have the following benefits: (I haven't tried it though)PickType
, we can now delegate entirely toInterfaceType
(sometimes combined with aUnionType
when the input type is a union) from within thepick
functionwithOptional
then returns anInterfaceType
instead of anIntersectionType
makes sense I guess?1strictMissingKeys
etcIf we decide to do that, we should do so in another branch. Let's discuss that offline.
Footnotes
Currently, objects with both mandatory and optional properties are implemented as an intersection of one object with the mandatory properties and one object with the optional properties. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nieuwe issue voor aangemaakt #93
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels kinda nasty, but I didn't want to touch the implementation of
UnionType
to deal with finding the maximum pick type.UnionType
seems to just find the first matching type when parsing and stringifying, so when that happens to be a narrower type in the union, you get unexpected results (see thePick<UnionAC, 'a' | 'd'>
test case). The second conversion would also stripd
, which is weird. Same would happen with stringify.By sorting this way, we guarantee that we don't strip too much of the properties accidentally as it will always find a biggest match first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting idea. If we do something like this, it should be part of the
UnionType
implementation. I agree that this is a major footgun, so I'm currently considering starting with apick
operation on simpler interface types only.I need to think and test your idea and possible other solutions to this footgun. We have had situations in our codebase where this happened, so I'm really happy if we can find a way to prevent "accidents" because of this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah so for example:
Still a bit in doubt about this, as the first name does show the distributive property much clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exporting these and the helper functions below only for testing, but that feels a bit odd. Is there a better way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one I'm using in pick now, but also don't know if it's okay to export just like that.