-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Align with the optional chaining spec #34385
Copy link
Copy link
Closed
Labels
area: compilerIssues related to `ngc`, Angular's template compilerIssues related to `ngc`, Angular's template compilerarea: coreIssues related to the framework runtimeIssues related to the framework runtimecore: binding & interpolationIssue related to property/attribute binding or text interpolationIssue related to property/attribute binding or text interpolationfeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issues
Milestone
Metadata
Metadata
Assignees
Labels
area: compilerIssues related to `ngc`, Angular's template compilerIssues related to `ngc`, Angular's template compilerarea: coreIssues related to the framework runtimeIssues related to the framework runtimecore: binding & interpolationIssue related to property/attribute binding or text interpolationIssue related to property/attribute binding or text interpolationfeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issues
Type
Fields
Give feedbackNo fields configured for issues without a type.
🚀 feature request
Relevant Package
@angular/compilerDescription
Optional chaining[1] reached stage 4. We've been supporting similar syntax in templates for a while now, calling it the "safe navigation operator"[2]. For simplicity and smaller payload, we can consider aligning with the spec in future versions of the framework.
There are a couple of semantical and syntactical differences between optional chaining and safe navigation.
Syntax
Optional chaining has the following syntax:
Safe navigation supports only direct property access. Optional chaining supports this, as well as, method calls and function calls. Function calls are particularly useful in iterators:
Semantics
With optional chaining, the expression
a?.bwill be translated toa == null ? undefined : a.b. In Angular, the semantics of the same expression would benull == a ? null : a.b.If
aisnullorundefined, the expressiontypeof a?.bwould evaluate to"object"with optional chaining and"undefined"in Angular's safe navigation operator.Except the mentioned difference above, method calls are compiled similarly:
In both, optional chaining and safe navigation in templates, stacking the operators is translated the same way:
(a?.b).c?.dbecomesnull == a ? null : null == a.b.c ? null : a.b.c.d.Another difference seems to be the way parentheses are handled. The optional chaining spec defines that
null==e.foo?null:e.foo.b.cshould be translated to(a == null ? undefined : a.b).c. In Angular the same expression translates tonull == a ? null : a.b.c.PS: looks like the last issue is fixed by #34221.
[1] Optional chaining spec https://github.com/tc39/proposal-optional-chaining
[2] Safe navigation https://angular.io/guide/template-syntax#safe-navigation-operator