-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Ensure method calls on unions get accurate types #4309
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
Open
mnsaglam
wants to merge
2
commits into
google:master
Choose a base branch
from
KimlikDAO:method_on_union
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -676,8 +676,8 @@ ReadonlyArray.prototype.join = function(opt_separator) {}; | |
| /** | ||
| * Extracts a section of an array and returns a new array. | ||
| * | ||
| * @param {?number=} begin Zero-based index at which to begin extraction. | ||
| * @param {?number=} end Zero-based index at which to end extraction. slice | ||
| * @param {number=} begin Zero-based index at which to begin extraction. | ||
| * @param {number=} end Zero-based index at which to end extraction. slice | ||
| * extracts up to but not including end. | ||
| * @return {!Array<T>} | ||
| * @this {IArrayLike<T>|string} | ||
|
|
@@ -906,8 +906,8 @@ Array.prototype.shift = function() {}; | |
| /** | ||
| * Extracts a section of an array and returns a new array. | ||
| * | ||
| * @param {?number=} begin Zero-based index at which to begin extraction. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here. |
||
| * @param {?number=} end Zero-based index at which to end extraction. slice | ||
| * @param {number=} begin Zero-based index at which to begin extraction. | ||
| * @param {number=} end Zero-based index at which to end extraction. slice | ||
| * extracts up to but not including end. | ||
| * @return {!Array<T>} | ||
| * @this {IArrayLike<T>|string} | ||
|
|
@@ -1188,13 +1188,13 @@ Number.prototype.toPrecision = function(opt_precision) {}; | |
| /** | ||
| * Returns a string representing the number. | ||
| * @this {Number|number} | ||
| * @param {(number|Number)=} opt_radix An optional radix. | ||
| * @param {number=} radix An optional radix. | ||
| * @return {string} | ||
| * @nosideeffects | ||
| * @see http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString | ||
| * @override | ||
| */ | ||
| Number.prototype.toString = function(opt_radix) {}; | ||
| Number.prototype.toString = function(radix) {}; | ||
|
|
||
| // Properties. | ||
| /** | ||
|
|
@@ -2178,13 +2178,13 @@ String.prototype.search = function(pattern) {}; | |
|
|
||
| /** | ||
| * @this {String|string} | ||
| * @param {number} begin | ||
| * @param {number=} opt_end | ||
| * @param {number=} begin | ||
| * @param {number=} end | ||
| * @return {string} | ||
| * @nosideeffects | ||
| * @see http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice | ||
| */ | ||
| String.prototype.slice = function(begin, opt_end) {}; | ||
| String.prototype.slice = function(begin, end) {}; | ||
|
|
||
| /** | ||
| * @this {String|string} | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
As far as I can tell removing the nullability of these params is causing lots of test breakage within Google. How critical is that to your PR / overall efforts?
We might be able to land the addition of optionality, but generally removal of nullability isn't something we can land given we don't have the capacity to fix up Google at this time.
Uh oh!
There was an error while loading. Please reload this page.
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.
Oh I see. I ran all the tests available in the repo using
bazelisk test //:alland I thought that would basically cover most situations.In the current situation, the type inference bails to * when .slice() over a union. Is there nothing in the repo that takes a slice over a union type like
(!Uint8Array|!Array<number>)though? I thought this would be a common utility method signature and instrictCheckTypesmode this is a warning / error.Currently the function merging requires exact argument signature match (everything except this: type and return type should be precisely the same).
closure-compiler/src/com/google/javascript/rhino/jstype/FunctionType.java
Lines 847 to 858 in 7ee76c7
In reality that can be relaxed quite a bit: for instance in
leastSuper = truemode, we can take the set intersection of each coordinate (for each argument length in presence of optional arguments and take union over argument length). Such as precise merge would let the correct inference happen with the current externs typings.How about (bigint|number).toString(radix). Do you think that can be implemented?
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.
Maybe all "well-written" code situations yes, but these definitions are synced into google3 and are used to compile ~all JavaScript at google3, and much of what is left that is in JavaScript is old and crufty and full of type-lies and so on.
Entirely possible, just not typed like that? It could already be typed as
Array<*>I don't yet see any issues with that, but it would be easiest for me to test that out if you separate that into a separate PR.