Title: Optional chaining with non null operator is unsafe, because it could throw an exception · Issue #36031 · microsoft/TypeScript · GitHub
Open Graph Title: Optional chaining with non null operator is unsafe, because it could throw an exception · Issue #36031 · microsoft/TypeScript
X Title: Optional chaining with non null operator is unsafe, because it could throw an exception · Issue #36031 · microsoft/TypeScript
Description: TypeScript Version: 3.7.2 Search Terms: optional chaining, non null operator Code This input a?.b!.c will compile to ((_a = a) === null || _a === void 0 ? void 0 : _a.b).c; But it's unsafe, because if a is null or undefined, will throw a...
Open Graph Description: TypeScript Version: 3.7.2 Search Terms: optional chaining, non null operator Code This input a?.b!.c will compile to ((_a = a) === null || _a === void 0 ? void 0 : _a.b).c; But it's unsafe, because...
X Description: TypeScript Version: 3.7.2 Search Terms: optional chaining, non null operator Code This input a?.b!.c will compile to ((_a = a) === null || _a === void 0 ? void 0 : _a.b).c; But it's unsafe, bec...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/36031
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Optional chaining with non null operator is unsafe, because it could throw an exception","articleBody":"**TypeScript Version:** 3.7.2\r\n\r\n**Search Terms:** optional chaining, non null operator\r\n\r\n**Code**\r\n\r\nThis input\r\n\r\n```ts\r\na?.b!.c\r\n```\r\n\r\nwill compile to\r\n\r\n```js\r\n((_a = a) === null || _a === void 0 ? void 0 : _a.b).c;\r\n```\r\n\r\nBut it's unsafe, because if `a` is `null` or `undefined`, will throw an exception:\r\n\r\n```js\r\n ((_a = a) === null || _a === void 0 ? void 0 : _a.b).c\r\n=\u003e ((_a = null) === null || _a === void 0 ? void 0 : _a.b).c\r\n=\u003e (null === null || _a === void 0 ? void 0 : _a.b).c\r\n=\u003e (true ? void 0 : _a.b).c\r\n=\u003e undefined.c\r\n```\r\n\r\nIMO we should not raise this exception.\r\n\r\n**Expected behavior:**\r\n\r\nWould be better to compile to\r\n\r\n```js\r\n(_a = a) === null || _a === void 0 ? void 0 : _a.b.c;\r\n```\r\n\r\nSo `a` could be null or undefined and it'll not raise an exception anymore.\r\n\r\n**Playground Link:** [Playground](http://www.typescriptlang.org/play/#code/DYUwLgBAhgXNB2BPAUFA-AOgEYEIMGMg)\r\n\r\n**Related Issues:** There was a lot of discussion here, but was more focused in the type system, not about the code output that raise wrongly an exception.\r\n- https://github.com/microsoft/TypeScript/issues/34875\r\n- https://github.com/microsoft/TypeScript/issues/35025\r\n- https://github.com/microsoft/TypeScript/issues/35071\r\n\r\nAlso this bug was reported in Babel and already there is a PR to fix that in Babel:\r\n- issue: https://github.com/babel/babel/issues/10959\r\n- pr: https://github.com/babel/babel/pull/10961\r\n\r\n**In this issue I'm saying only about the output, not about the type system.**","author":{"url":"https://github.com/macabeus","@type":"Person","name":"macabeus"},"datePublished":"2020-01-06T18:07:13.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":23},"url":"https://github.com/36031/TypeScript/issues/36031"}
| 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:5da0a1b8-3a86-6b4d-2d8a-e8ca3091fd5a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8206:2052BE:17CB454:20AE885:6A53E519 |
| html-safe-nonce | bb2d13c41bd24fc43af2600e800ec21c4da13a3441ef98495c3d9d652098455a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MjA2OjIwNTJCRToxN0NCNDU0OjIwQUU4ODU6NkE1M0U1MTkiLCJ2aXNpdG9yX2lkIjoiNjg0NjQ5Njg5MzE4ODY5NTMyMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | dd89f289b5ba393b28ce1c0b8133485d83618534b9dbc229e92ee5b45ee190c7 |
| hovercard-subject-tag | issue:545861573 |
| 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/microsoft/TypeScript/36031/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c046c18ec3a9f127e9ed7bbec89f905e899e52a71e365b4e6edd3eed51cc9568/microsoft/TypeScript/issues/36031 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c046c18ec3a9f127e9ed7bbec89f905e899e52a71e365b4e6edd3eed51cc9568/microsoft/TypeScript/issues/36031 |
| og:image:alt | TypeScript Version: 3.7.2 Search Terms: optional chaining, non null operator Code This input a?.b!.c will compile to ((_a = a) === null || _a === void 0 ? void 0 : _a.b).c; But it's unsafe, because... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | macabeus |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| 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 | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width