Title: Property initializers output for non-initialized types · Issue #45076 · microsoft/TypeScript · GitHub
Open Graph Title: Property initializers output for non-initialized types · Issue #45076 · microsoft/TypeScript
X Title: Property initializers output for non-initialized types · Issue #45076 · microsoft/TypeScript
Description: Bug Report This is technically not a bug, given ES Spec, however, I'm filing an issue in case an consideration needs to be made on how to handle with regard to documentation, etc, and/or to provide a solution for people facing the same. ...
Open Graph Description: Bug Report This is technically not a bug, given ES Spec, however, I'm filing an issue in case an consideration needs to be made on how to handle with regard to documentation, etc, and/or to provide...
X Description: Bug Report This is technically not a bug, given ES Spec, however, I'm filing an issue in case an consideration needs to be made on how to handle with regard to documentation, etc, and/or to pro...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/45076
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Property initializers output for non-initialized types","articleBody":"# Bug Report\r\n\r\nThis is technically not a bug, given ES Spec, however, I'm filing an issue in case an consideration needs to be made on how to handle with regard to documentation, etc, and/or to provide a solution for people facing the same.\r\n\r\n**Issue Summary**\r\n\r\n- ESNext spec seems to call for defining all class fields. This also affects those which are optional or specified with a boom.\r\n- Spec also dictates that property initializers are called _after_ the super call. (https://github.com/tc39/proposal-class-fields)\r\n\r\nThis results in any properties specified in the inherited class that are assigned during the base class constructor to be overwritten, even if they do not have initializer values specified in the inherited class.\r\n\r\nThe behaviour which causes the error can be seen below:\r\n\r\n```ts\r\nclass A {\r\n a?: string\r\n}\r\n```\r\n\r\nOutput is:\r\n\r\n```js\r\n// target = ESNext\r\nclass A {\r\n a; // Outputs a statement without initialized value, which is treated as a = undefined;\r\n}\r\n\r\n// target = ES2020\r\nclass A {\r\n}\r\n```\r\n\r\n**Here is a simplified version of how this affected me**\r\n\r\n```ts\r\nabstract class Base\u003cT extends Record\u003cstring, any>> {\r\n // Single constructor for all derivatives\r\n constructor(o: Omit\u003cT, typeof Base>) {\r\n Object.assign(this, o);\r\n }\r\n}\r\n\r\nclass A extends Base\u003cA> {\r\n myProp?: string\r\n myProp2!: string\r\n}\r\n\r\nconst a = new A({ myProp2: 'hello' });\r\n\r\n// ESNext: a = { myProp2: undefined, myProp: undefined }\r\n// ES2020: a = { myProp2: 'hello' }\r\n```\r\n\r\nThe above produces:\r\n\r\n```ts\r\n// TypeScript target=ESNext\r\nclass A extends Base {\r\n myProp;\r\n myProp2; // Note the boomed property still gets output here, so it is initialized after the super call - which maybe surprising behaviour\r\n}\r\n\r\n// ES2020 / Babel\r\nclass A extends Base {\r\n}\r\n```\r\n\r\n### 🔎 Search Terms\r\n\r\n- property initializers\r\n\r\n### 🕗 Version & Regression Information\r\n\r\nTS 4.3.5\r\n\r\n## Solution\r\n\r\nFor those facing this issue, simply change property declarations to ambient.\r\n\r\nie:\r\n\r\n```ts\r\nclass A extends Base\u003cA> {\r\n declare myProp?: string\r\n declare myProp2: string\r\n}\r\n```","author":{"url":"https://github.com/nonara","@type":"Person","name":"nonara"},"datePublished":"2021-07-16T21:20:19Z","url":"https://github.com/microsoft/TypeScript/issues/45076"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| 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:794ab9d1-2939-ff51-682d-c78ebb278220 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D3AC:E530F:12A35AB:18CC76A:6AED5E1C |
| html-safe-nonce | e90a45b3eda7ab29333d2b63182d6648e7cc4dbc8382107770cd2d6323bb8c7d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEM0FDOkU1MzBGOjEyQTM1QUI6MThDQzc2QTo2QUVENUUxQyIsInZpc2l0b3JfaWQiOiI0NDY5Njg5MjA4ODAwMTA4MDYwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 15f3872723c273315ae4d569b1b7714456ef55f79e802fc5cb233668b6f0aef3 |
| hovercard-subject-tag | issue:946603593 |
| 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/45076/issue_layout |
| twitter:image | https://opengraph.githubassets.com/12916a76d3ce14976d82e3e2dbed7a295f8255e6b355b29ecf318261d9acccf0/microsoft/TypeScript/issues/45076 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/12916a76d3ce14976d82e3e2dbed7a295f8255e6b355b29ecf318261d9acccf0/microsoft/TypeScript/issues/45076 |
| og:image:alt | Bug Report This is technically not a bug, given ES Spec, however, I'm filing an issue in case an consideration needs to be made on how to handle with regard to documentation, etc, and/or to provide... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nonara |
| hostname | github.com |
| expected-hostname | github.com |
| None | 56fb42677ae87ed3e64410323d67980aa65d0485568a7858f874e7edfc6d1c11 |
| 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 | 9c9a37e2423ed382cf39f70b5aa4b4858b40a3b0 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width