Implement constructor check type guard#32256
Closed
austincummings wants to merge 1 commit into
Closed
Conversation
austincummings
commented
Jul 4, 2019
| bar.prop1; // string | ||
| bar.prop2; // number | ||
| } | ||
| if (bar.constructor === Foo) { |
Contributor
Author
There was a problem hiding this comment.
As far as I can tell, this should always be false. Right now the type get's narrowed to Bar in the if, is this right? Or should it narrow just to Foo?
There was a problem hiding this comment.
Shouldn't it narrow to never inside the if since Bar | boolean can never satisfy the condition?
Contributor
Author
There was a problem hiding this comment.
@fatcerberus That's a good point. I didn't think about narrowing to never. Seems like the thing to do. I'll change this. Thanks.
austincummings
commented
Jul 4, 2019
| } | ||
| return type; | ||
|
|
||
| function isConstructorAccessExpression(expr: Expression): expr is AccessExpression { |
Contributor
Author
There was a problem hiding this comment.
Is there somewhere else I should put this or is this ok?
austincummings
commented
Jul 4, 2019
| return ( | ||
| isPropertyAccessEntityNameExpression(expr) && idText(expr.name) === "constructor" | ||
| || isElementAccessExpression(expr) && isStringLiteralLike(expr.argumentExpression) && expr.argumentExpression.text === "constructor" | ||
| ); |
Contributor
Author
There was a problem hiding this comment.
Not sure on the formatting of this...
Contributor
Author
|
Closing to reopen a fresh PR with much more detail. |
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fixes #23274
This builds on work done by @Kingwl in PR #23622 to add a new type guard checking for the
instance.constructor === Constructorsyntactic pattern to narrow the type ofinstance.I'm making this a draft PR as there's a couple things I need some guidance on.
Thanks!