Control flow analysis of aliased conditions is not able to narrow object properties
See original GitHub issueSuggestion
🔍 Search Terms
- Control flow
- Aliased conditions
- Type narrowing
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
⭐ Suggestion
TypeScript 4.4 added “Control Flow Analysis of Aliased Conditions and Discriminants” which is a great feature, but unfortunately it seems unable to narrow the types on object properties which limits the benefit.
📃 Motivating Example
Take null checks for instance, the following code results in a compiler warning in the AliasedControlFlow
-method. The RegularControlFlow
method works fine even though it does the exakt same comparison.
interface ITest {
prop?: string;
}
function AliasedControlFlow(test: ITest) {
const hasProp = test.prop != null;
// Object is possibly 'undefined'.
return hasProp ? test.prop.big() : "";
}
function RegularControlFlow(test: ITest) {
return test.prop != null ? test.prop.big() : "";
}
💻 Use Cases
This would really help when converting code that is currently not using strictNullChecks
as null checks may already be aliased in existing code.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Control Flow Analysis of Aliased Conditions and Discriminants ...
Using if (argIsString) correctly narrows arg to type string. Using if (argIsString === true) however does not narrow the arg parameter to string ......
Read more >Control flow analysis of aliased conditional expressions in ...
Whilst an aliased conditional expression on a destructured field will allow for narrowing the original object's type, the flow analysis ...
Read more >Why doesn't a type narrowing check of a class property not ...
1 Answer 1 ... Control Flow Analysis of Aliased Conditions and Discriminants was only recently added, and it has limitations, including: Narrowing ......
Read more >Type Guards and Control Flow Analysis in TypeScript 4.4
Here we have a type guard against property nested 2 levels into an object but the compiler still narrows it. However, this does...
Read more >Get the best of TypeScript Control Flow Analysis - Retool
Narrowing based on multiple variables and conditions. The fact that TypeScript can infer variable types by looking at simple conditions is not ......
Read more >Top Related Medium Post
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s basically a cost/benefit trade-off in control flow analysis. In order to support mutable properties we’d have to check that there are no assignments to a property between the declaration of the aliased condition that references the property and the check of that aliased condition. It’s possible to do so (anything is possible), but it is non-trivial and it’s not clear the added complexity and potential performance cost is worth it.
The issue here is that
prop
is not a readonly property. From #44730, the PR that implemented the feature: