-
Notifications
You must be signed in to change notification settings - Fork 633
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
fix(collections): ensure pick
doesn't generate missing properties as undefined
#5926
Merged
+16
−1
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
153b4fb
fix(collections): ensure `pick` doesn't generate missing properties a…
yuhr cc79acf
style: format by `deno fmt`
yuhr 9314ac0
fix: fix(collections): ensure `pick` respect non-own properties as it…
yuhr c8c7c31
style: format by `deno fmt`
yuhr 5d6d1e4
refactor(collections): unuse `flatMap` in `pick`
yuhr 5d2fd7e
test: remove test case that is to ensure picking non-own properties
yuhr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
I personally don't think this is intentionally in this way.. (The check with
Object.hasOwn
meets the users' expectation better in my view)@lambdalisue What do you think?
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.
Considering the original purpose, I hadn't envisioned a use case where
toString
would be picked. Therefore, I think @kt3k's suggestion is a good one. However, since the implementation currently referencesobj[k]
, changing it toObject.hasOwn
would be a breaking change. Additionally, in terms of type expression, it's written asK in keyof T
, which means it allows something liketoString
(the following code does not result in a type error).This might be beyond the scope of this PR, but I realized that
o.foo()
does not work in the code above. Thus, currently there is an inconsistency betweenpick
andomit
. Given this situation, we need to choose between the following options:pick
andomit
toObject.hasOwn
(and adjust the type so it expresses this correctly).omit
so thato.foo()
does not result in a runtime error.Personally, I prefer option 1, but I’m not sure how to express this with types.
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.
I actually agree with @kt3k, the test case is just to avoid introducing accidental breaking change in the meantime.
I appreciate all the consideration given by @lambdalisue. Especially, the inconsistency between
pick
andomit
is one of the reasons I proposed #5927; the most beautiful solution I think is to only operate on own properties and copy the prototype to the result, which is kind of a merge of option 1 and 2. Try replacing the imports with the following code and rerun your example code:But yes, the parameter types will be looser and the return types will be stricter than desired this way, though still sound.
Anyways it's beyond the scope, so I suggest moving to another issue.
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.
Ah, ok. The current check (
key in obj
) works good for the case of the aboveclass A
example (foo
method should be picked in that case).I'm now in favor of landing this fix.
However, I'm still not sure we should include this particular example (
pick(obj, ["toString"])
) as a test case, as this causes type error if we don't type castobj
to any. I think we can consider this particular behavior something like undefined/not decided behavior.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.
How about just removing this test case and revisit later when someone comes up with something better?
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.
I'm not opposed to it, but just curious, is the concept of “undefined behavior” common in the standard library? If we decide to make this UB, there has to be an explicit notice in the JSDoc comment.
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.
It's not common to make something explicitly undefined behavior in the standard library, but I think we are not ready to decide about this behavior, and also this looks out of scope of this PR.
In my view, in the below example,
pick
ingfoo
looks fine, butpick
ingtoString
looks strange as it doesn't match its type (it's actually causes a type error).