Title: Align with the optional chaining spec · Issue #34385 · angular/angular · GitHub
Open Graph Title: Align with the optional chaining spec · Issue #34385 · angular/angular
X Title: Align with the optional chaining spec · Issue #34385 · angular/angular
Description: 🚀 feature request Relevant Package @angular/compiler Description 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 sm...
Open Graph Description: 🚀 feature request Relevant Package @angular/compiler Description Optional chaining[1] reached stage 4. We've been supporting similar syntax in templates for a while now, calling it the "safe naviga...
X Description: 🚀 feature request Relevant Package @angular/compiler Description Optional chaining[1] reached stage 4. We've been supporting similar syntax in templates for a while now, calling it the "sa...
Opengraph URL: https://github.com/angular/angular/issues/34385
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Align with the optional chaining spec","articleBody":"# 🚀 feature request\r\n\r\n### Relevant Package\r\n\r\n`@angular/compiler`\r\n\r\n### Description\r\n\r\n[Optional chaining](https://github.com/tc39/proposal-optional-chaining)[1] reached stage 4. We've been supporting similar syntax in templates for a while now, calling it the [\"safe navigation operator\"](https://angular.io/guide/template-syntax#safe-navigation-operator)[2]. For simplicity and smaller payload, we can consider aligning with the spec in future versions of the framework.\r\n\r\nThere are a couple of semantical and syntactical differences between optional chaining and safe navigation.\r\n\r\n## Syntax \r\n\r\nOptional chaining has the following syntax:\r\n\r\n```ts\r\nobj?.prop // optional static property access\r\nobj?.[expr] // optional dynamic property access\r\nfunc?.(...args) // optional function or method call\r\n```\r\n\r\nSafe 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:\r\n\r\n```ts\r\niterator.return?.()\r\n```\r\n\r\n## Semantics\r\n\r\nWith optional chaining, the expression `a?.b` will be translated to `a == null ? undefined : a.b`. In Angular, the semantics of the same expression would be `null == a ? null : a.b`.\r\n\r\nIf `a` is `null` or `undefined`, the expression `typeof a?.b` would evaluate to `\"object\"` with optional chaining and `\"undefined\"` in Angular's safe navigation operator.\r\n\r\nExcept the mentioned difference above, method calls are compiled similarly:\r\n\r\n```ts\r\na?.b()\r\na == null ? undefined : a.b()\r\n```\r\n\r\nIn both, optional chaining and safe navigation in templates, stacking the operators is translated the same way: `(a?.b).c?.d` becomes `null == a ? null : null == a.b.c ? null : a.b.c.d`.\r\n\r\nAnother difference seems to be the way parentheses are handled. The optional chaining spec defines that `null==e.foo?null:e.foo.b.c` should be translated to `(a == null ? undefined : a.b).c`. In Angular the same expression translates to `null == a ? null : a.b.c`.\r\n\r\nPS: looks like the last issue is fixed by https://github.com/angular/angular/pull/34221.\r\n\r\n---\r\n\r\n[1] Optional chaining spec https://github.com/tc39/proposal-optional-chaining\r\n[2] Safe navigation https://angular.io/guide/template-syntax#safe-navigation-operator","author":{"url":"https://github.com/mgechev","@type":"Person","name":"mgechev"},"datePublished":"2019-12-12T22:36:42Z","url":"https://github.com/angular/angular/issues/34385"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c38c7156-bc3f-728b-5282-29a65ce4cb8b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A0A8:2564F:16B2999:1DE440A:6B0130BA |
| html-safe-nonce | 8ab7e2ced09bfcb7c142bccd786a5008e8d717a8a1bc1a76fc2020ba18b1a0ec |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMEE4OjI1NjRGOjE2QjI5OTk6MURFNDQwQTo2QjAxMzBCQSIsInZpc2l0b3JfaWQiOiI3NDYwODAzOTk5NjY3NjYzMDM1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5d5478ead23a8e367db653cba2607b1ba6befc206c1256561fd880501b5968f2 |
| hovercard-subject-tag | issue:537250738 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/34385/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5c719ce2e1bb1feea33b3eaadcf782dbe74578ad83d25ea473b55f1e9ab45abd/angular/angular/issues/34385 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5c719ce2e1bb1feea33b3eaadcf782dbe74578ad83d25ea473b55f1e9ab45abd/angular/angular/issues/34385 |
| og:image:alt | 🚀 feature request Relevant Package @angular/compiler Description Optional chaining[1] reached stage 4. We've been supporting similar syntax in templates for a while now, calling it the "safe naviga... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mgechev |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width