Title: Transpilation of optional chaining combined with type casting results in function call losing its context · Issue #44639 · microsoft/TypeScript · GitHub
Open Graph Title: Transpilation of optional chaining combined with type casting results in function call losing its context · Issue #44639 · microsoft/TypeScript
X Title: Transpilation of optional chaining combined with type casting results in function call losing its context · Issue #44639 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms optional chaining, type casting, transpiling 🕗 Version & Regression Information When transpiling the optional chaining feature on a method class and combining it to a type casting, the generated code makes the f...
Open Graph Description: Bug Report 🔎 Search Terms optional chaining, type casting, transpiling 🕗 Version & Regression Information When transpiling the optional chaining feature on a method class and combining it to a type...
X Description: Bug Report 🔎 Search Terms optional chaining, type casting, transpiling 🕗 Version & Regression Information When transpiling the optional chaining feature on a method class and combining it to a ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/44639
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Transpilation of optional chaining combined with type casting results in function call losing its context","articleBody":"# Bug Report\r\n\r\n\u003c!--\r\n Please fill in each section completely. Thank you!\r\n-->\r\n\r\n### 🔎 Search Terms\r\n\r\noptional chaining, type casting, transpiling\r\n\r\n### 🕗 Version & Regression Information\r\n\r\nWhen transpiling the optional chaining feature on a method class and combining it to a type casting, the generated code makes the function call loses its context (`this`).\r\n\r\nThe same does not occur when transpiling the optional chaining feature without the type casting, or if you use type casting without transpiled optional chaining.\r\n\r\nThe behavior is the same since optional chaining was introduced to TypeScript since version 3.7. \r\n\r\n### ⏯ Playground Link\r\n\r\n\u003c!--\r\n A link to a TypeScript Playground \"Share\" link which shows this behavior\r\n\r\n The TypeScript Workbench can be used for more complex setups, try\r\n https://www.typescriptlang.org/dev/bug-workbench/\r\n\r\n As a last resort, you can link to a repo, but these will be slower for us to investigate.\r\n-->\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?target=6&ts=3.7.5&strict=true#code/MYGwhgzhAEAqCmEAu9oG8BQ1vRc+AFAJTpY7kBO8SArhQHbQBsLA3GdgL4bfAD29ZLmgBeaPXgB3OIhTF2GAPSLoAIRoBzDQE9oAI3gALMADcAlnwoAuJSqTaADqmCQkZ+huhh6AE2h8HNwEwEGhgY3d3T34AWz13eD8YsABrRGgAMxp6YCDGFxBQkD4IdPdkb2BnARQADyQMAiQAOjwULxhvbSIAfmb5DFtoAGFLKlz9I1MLCggbZVxDVAgwGNQM+DBaKhgaUr93HzNzHxoQkF0fPnT6PiRoZLTF9ezci3zz6GLSmHKkSuq9DqDSarVkqEgXno3QGoLa8F6-SI7CAA)\r\n\r\n### 💻 Code\r\n\r\nOriginal TypeScript code:\r\n\r\n```ts\r\nclass Teste {\r\n teste() {\r\n return 666;\r\n }\r\n}\r\nconst t = new Teste();\r\n\r\n// Buggy behavior:\r\n// type casting and optional chaining combined makes function call loses instance context\r\n(t.teste as any)?.();\r\n\r\n// Correct behaviors:\r\n// the same features used individually does not make the function call loses instance context\r\n(t.teste as any)();\r\n(t.teste)?.();\r\n```\r\n\r\nTranspiles to:\r\n\r\n```js\r\n\"use strict\";\r\nvar _a, _b, _c;\r\nclass Teste {\r\n teste() {\r\n return 666;\r\n }\r\n}\r\nconst t = new Teste();\r\n\r\n// Buggy behavior:\r\n// type casting and optional chaining combined makes function call loses instance context\r\n(_a = t.teste) === null || _a === void 0 ? void 0 : _a();\r\n\r\n// Correct behaviors:\r\n// the same features used individually does not make the function call loses instance context\r\nt.teste();\r\n(_c = ((_b = t).teste)) === null || _c === void 0 ? void 0 : _c.call(_b);\r\n```\r\n\r\n\r\n### 🙂 Expected behavior\r\n\r\nI believe that type casting should not make transpilation generates different code with different behavior for optional chaining or any other feature.\r\n","author":{"url":"https://github.com/willgm","@type":"Person","name":"willgm"},"datePublished":"2021-06-17T22:37:09Z","url":"https://github.com/microsoft/TypeScript/issues/44639"}
| 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:da6dcc67-1158-3c93-e6cd-862ef0724057 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DFB4:A3C75:45CAD:57543:6B00F1A0 |
| html-safe-nonce | 8ea438f32c61b1ac3299ac88fbf6a8a1d033836aad86bb5d065f0e6adaf3be09 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERkI0OkEzQzc1OjQ1Q0FEOjU3NTQzOjZCMDBGMUEwIiwidmlzaXRvcl9pZCI6IjI0NTM4MDgyNzY0ODgzODQ5MjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 281d8854ce2244d988eb7a5709728df3dd0fd052f4bc89b02c784e6c9669f5ad |
| hovercard-subject-tag | issue:924390802 |
| 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/44639/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2d1de0414299061c0063e2221fd02b34a8473b40ae86f9d592bbbb881a3a3925/microsoft/TypeScript/issues/44639 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2d1de0414299061c0063e2221fd02b34a8473b40ae86f9d592bbbb881a3a3925/microsoft/TypeScript/issues/44639 |
| og:image:alt | Bug Report 🔎 Search Terms optional chaining, type casting, transpiling 🕗 Version & Regression Information When transpiling the optional chaining feature on a method class and combining it to a type... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | willgm |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width