Repository navigation
Control flow analysis of aliased conditions is not able to narrow object properties #46412
Description
Activity
The issue here is that
propis not a readonly property. From #44730, the PR that implemented the feature:Narrowing through indirect references occurs only when the conditional expression or discriminant property access is declared in a
constvariable declaration with no type annotation, and the reference being narrowed is aconstvariable, areadonlyproperty, or a parameter for which there are no assignments in the function body.Reacted by Martin Johns, David Zidar and Olov Schedin- addedDesign LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
on Oct 18, 2021 Anders Hejlsberg (@ahejlsberg) Thank you for the response! In this case
readonlycan not be used as it is an object.'readonly' type modifier is only permitted on array and tuple literal types.
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
readonlydid not work but theReadonly<T>mapped type does work, it may take care of some of the use cases as long as all properties on the type can be readonly.MartinJohns commented
on Oct 18, 2021 ContributorMore actionsThe property must be marked
readonly:interface ITest { readonly prop?: string; }
Martin Johns (@MartinJohns) Right, I understand, that's what
Readonly<T>does when used on the function argument.Is this a use case that is intended to be fixed in the future or is the limitation too much work to resolve?
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.
Reacted by Filipe Beck, David Zidar and Olov SchedinAnders Hejlsberg (@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. TheRegularControlFlowmethod works fine even though it does the exakt same comparison.💻 Use Cases
This would really help when converting code that is currently not using
strictNullChecksas null checks may already be aliased in existing code.