Title: Warn on non-nullable optional chain · Issue #44870 · angular/angular · GitHub
Open Graph Title: Warn on non-nullable optional chain · Issue #44870 · angular/angular
X Title: Warn on non-nullable optional chain · Issue #44870 · angular/angular
Description: Which @angular/* package(s) are relevant/releated to the feature request? compiler-cli Description Optional chaining is a great way to safely access properties that might be null or undefined, for example: import {Component, Input} from ...
Open Graph Description: Which @angular/* package(s) are relevant/releated to the feature request? compiler-cli Description Optional chaining is a great way to safely access properties that might be null or undefined, for ...
X Description: Which @angular/* package(s) are relevant/releated to the feature request? compiler-cli Description Optional chaining is a great way to safely access properties that might be null or undefined, for ...
Opengraph URL: https://github.com/angular/angular/issues/44870
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Warn on non-nullable optional chain","articleBody":"### Which @angular/* package(s) are relevant/releated to the feature request?\n\ncompiler-cli\n\n### Description\n\nOptional chaining is a great way to safely access properties that might be `null` or `undefined`, for example:\r\n\r\n```typescript\r\nimport {Component, Input} from '@angular/core';\r\n\r\n@Component({\r\n template: `\r\n \u003cspan>Hello, {{ user?.name ?? 'guest' }}!\u003c/span>\r\n `,\r\n})\r\nexport class Greeter {\r\n @Input() user?: User;\r\n}\r\n\r\ninterface User {\r\n name: string;\r\n}\r\n```\r\n\r\nBut things can easily change and make the optional chain useless, such as:\r\n\r\n```diff\r\n export class Greeter {\r\n- @Input() user?: User;\r\n+ @Input() user: User;\r\n }\r\n```\r\n\r\nThis change means that the optional chain `user?.name` is no longer useful because `user` will never be `null` or `undefined`. This leads to confusion and tricks readers of the code into thinking that there is a case where \"Hello, guest!\" is displayed, when that actually isn't the case. If the default \"guest\" string comes from somewhere else, it could lead to dead code and possibly result in excess bundle size.\n\n### Proposed solution\n\nWe should consider an extended diagnostic to identify and warn developers in this situation. We already have [a diagnostic to warn about useless nullish coalescing operators `??`](https://angular.io/extended-diagnostics/NG8102), so it makes sense to have a similar check for optional chaining.\n\n### Alternatives considered\n\nN/A","author":{"url":"https://github.com/dgp1130","@type":"Person","name":"dgp1130"},"datePublished":"2022-01-28T02:29:53Z","url":"https://github.com/angular/angular/issues/44870"}
| 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:e990a53c-0d90-91e6-4013-eb1eabe5cf14 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ABE0:882E2:C1B649:101043E:6B00EFEA |
| html-safe-nonce | f523732b4f0071012c1ad3a483c7e0f2cb633c353512e6889dabfc2aa2ca7777 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQkUwOjg4MkUyOkMxQjY0OToxMDEwNDNFOjZCMDBFRkVBIiwidmlzaXRvcl9pZCI6IjYyOTk5MTIxODI1MTU2OTE0OTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9064432371aa488c57c0fc0a213884f305aaa76fc19c2ac4e1915717d02a057b |
| hovercard-subject-tag | issue:1116935779 |
| 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/44870/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f2494ba5bb319080a7c8f1c84f22619b83ea38fbd1d47238956fbc698e700996/angular/angular/issues/44870 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f2494ba5bb319080a7c8f1c84f22619b83ea38fbd1d47238956fbc698e700996/angular/angular/issues/44870 |
| og:image:alt | Which @angular/* package(s) are relevant/releated to the feature request? compiler-cli Description Optional chaining is a great way to safely access properties that might be null or undefined, for ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dgp1130 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width