Fixed narrowing of optional chains using type predicates#60120
Fixed narrowing of optional chains using type predicates#60120Andarist wants to merge 2 commits intomicrosoft:mainfrom
Conversation
| if (!assumeTrue && type.flags & TypeFlags.Union && optionalChainContainsReference(expr, reference)) { | ||
| const types = [narrowedType]; | ||
| if (containsUndefinedType(type)) { | ||
| types.push(undefinedType); | ||
| } | ||
| if (containsType((type as UnionType).types, nullType)) { | ||
| types.push(nullType); | ||
| } | ||
| return getUnionType(types); | ||
| } |
There was a problem hiding this comment.
I'm still thinking if this is the right place to "add back" those nullable types but in the meantime an extended test suite run could be insightful (however, this likely is not a popular pattern so maybe chances of it catching any potential issues is pretty low).
It feels like narrowTypeByCallExpression could be a better place for this... but it's much easier to do it here. narrowTypeByCallExpression receives a "flipped" assumeTrue so it isn't aware that it should not filter out null/undefined in what is the "true" !assumeTrue branch because it sees assumeTrue === true.
There was a problem hiding this comment.
note for myself: optionalChainContainsReference is too broad of a check, I should add tests with optional chains and identifier predicates
|
I'll run the tests, but the self test is failing which implies something wrong (in one way or another) @typescript-bot test it |
|
Hey @jakebailey, the results of running the DT tests are ready. Everything looks the same! |
|
@jakebailey Here are the results of running the user tests with tsc comparing Everything looks good! |
|
Repro of the self-check failure: TS playground. I'll investigate this soon. |
|
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@jakebailey Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
|
For now, I fixed the self-check by adding an explicit annotation. The regression found here is unfortunate but I don't think this is a new problem, just see this journey through examples: TS playground. It feels like this didn't error previously out of sheer luck. @jakebailey could you rerun perf tests? The current reported numbers for the self codebase are worrying but I don't see how this PR could affect parse or bind times. Was it just a fluke? 🤔 |
|
@typescript-bot perf test this faster |
|
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fixes #60106