-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Control flow analysis of aliased conditions is not able to narrow object properties #46412
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
Comments
The issue here is that
|
@ahejlsberg Thank you for the response! In this case
I'm not sure I understand the last part of the paragraph, there are no assignments in the function body except for the alias. Is this a use case that is intended to be fixed in the future or is the limitation too much work to resolve? |
The language keyword |
The property must be marked interface ITest {
readonly prop?: string;
} |
@MartinJohns Right, I understand, that's what |
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. |
@ahejlsberg I understand, that sounds reasonable. There could be a lot more code between the alias and the usage. Do I close this issue now or leave it open for further discussion? |
Suggestion
π Search Terms
β Viability Checklist
My suggestion meets these guidelines:
β 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. TheRegularControlFlow
method works fine even though it does the exakt same comparison.π» 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.The text was updated successfully, but these errors were encountered: