-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Grab the left type when narrowing by in operator lazily #54795
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
Grab the left type when narrowing by in operator lazily #54795
Conversation
@typescript-bot test this |
Heya @jakebailey, I've started to run the extended test suite on this PR at c225163. You can monitor the build here. |
Heya @jakebailey, I've started to run the diff-based user code test suite on this PR at c225163. You can monitor the build here. Update: The results are in! |
Heya @jakebailey, I've started to run the abridged perf test suite on this PR at c225163. You can monitor the build here. Update: The results are in! |
Heya @jakebailey, I've started to run the parallelized Definitely Typed test suite on this PR at c225163. You can monitor the build here. Update: The results are in! |
Heya @jakebailey, I've started to run the tarball bundle task on this PR at c225163. You can monitor the build here. |
Heya @jakebailey, I've started to run the diff-based top-repos suite on this PR at c225163. You can monitor the build here. Update: The results are in! |
Hey @jakebailey, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
@jakebailey Here are the results of running the user test suite comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Something interesting changed - please have a look. Details
|
@jakebailey Here are the results of running the top-repos suite comparing Everything looks good! |
Hey @jakebailey, it looks like the DT test run failed. Please check the log for more details. |
@jakebailey Here they are:Comparison Report - main..54795
System
Hosts
Scenarios
Developer Information: |
@typescript-bot run dt |
Heya @jakebailey, I've started to run the parallelized Definitely Typed test suite on this PR at c225163. You can monitor the build here. Update: The results are in! |
Hey @jakebailey, the results of running the DT tests are ready. |
src/compiler/checker.ts
Outdated
if (containsMissingType(type) && isAccessExpression(reference) && isMatchingReference(reference.expression, target) && (leftType = getTypeOfExpression(expr.left)).flags & TypeFlags.StringOrNumberLiteralOrUnique && | ||
getAccessedPropertyName(reference) === getPropertyNameFromType(leftType as StringLiteralType | NumberLiteralType | UniqueESSymbolType)) { | ||
return getTypeWithFacts(type, assumeTrue ? TypeFacts.NEUndefined : TypeFacts.EQUndefined); | ||
} |
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.
Since there is no else
you can just have two nested if
statements to avoid an assignment deep in a condition, and you can use isTypeUsableAsPropertyName
to avoid the cast:
if (containsMissingType(type) && isAccessExpression(reference) && isMatchingReference(reference.expression, target) && (leftType = getTypeOfExpression(expr.left)).flags & TypeFlags.StringOrNumberLiteralOrUnique && | |
getAccessedPropertyName(reference) === getPropertyNameFromType(leftType as StringLiteralType | NumberLiteralType | UniqueESSymbolType)) { | |
return getTypeWithFacts(type, assumeTrue ? TypeFacts.NEUndefined : TypeFacts.EQUndefined); | |
} | |
if (containsMissingType(type) && isAccessExpression(reference) && isMatchingReference(reference.expression, target)) { | |
const leftType = getTypeOfExpression(expr.left); | |
if (isTypeUsableAsPropertyName(leftType) && getAccessedPropertyName(reference) === getPropertyNameFromType(leftType)) { | |
return getTypeWithFacts(type, assumeTrue ? TypeFacts.NEUndefined : TypeFacts.EQUndefined); | |
} | |
} |
You can do the same for the other if
below this as well.
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.
adjusted the code as requested, I'm not sure why I've written it this way - it was bizarre 😅
fixes #54790