-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Adds empty object narrowing to type guards #4051
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
Conversation
// if (isA(x)) { // isA(...) returns A and the narrowing type has the value of A so narrow to void type. | ||
// | ||
// } | ||
// else if (isB(x)) { // narrow to A |
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.
You mean "to B"
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.
x is on the else clause. So it removes B from the union set.
if (!(type.flags & TypeFlags.Union)) { | ||
// Get the original type again because we don't want to store it on a variable higher | ||
// up to minimize memory allocation. | ||
let originalType = getTypeOfSymbol(symbol); |
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'd just get originalType
at the beginning with type
itself.
We'd prefer to use |
Ok just changed it to an empty object type. What's the reason behind |
// | ||
// let x: A | B | C; | ||
// | ||
// if (isB(x)) { // narrow x to A. |
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.
Do you mean A | C
?
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.
No I meant A. It remove B from A(which is A).
Is there more to be done here? |
Fixes #4029.
Void narrowing only affects union types: