Title: ES2022 Class Decorators not working when Class has self-static member · Issue #52004 · microsoft/TypeScript · GitHub
Open Graph Title: ES2022 Class Decorators not working when Class has self-static member · Issue #52004 · microsoft/TypeScript
X Title: ES2022 Class Decorators not working when Class has self-static member · Issue #52004 · microsoft/TypeScript
Description: Bug Report Class decorators are not working properly when the class has a static field of the same type and is instantiating it statically. Seems like this only happens when targeting ES2022. 🔎 Search Terms Various combinations of the be...
Open Graph Description: Bug Report Class decorators are not working properly when the class has a static field of the same type and is instantiating it statically. Seems like this only happens when targeting ES2022. 🔎 Sea...
X Description: Bug Report Class decorators are not working properly when the class has a static field of the same type and is instantiating it statically. Seems like this only happens when targeting ES2022. 🔎 Sea...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/52004
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ES2022 Class Decorators not working when Class has self-static member","articleBody":"# Bug Report\r\n\r\nClass decorators are not working properly when the class has a static field of the same type and is instantiating it statically. Seems like this only happens when targeting ES2022.\r\n\r\n### 🔎 Search Terms\r\n\r\nVarious combinations of the below search strings. Haven't been able to find anything useful.\r\nBeen searching for several days now, these are just the most recent ones I could remember:\r\n\r\n- Typescript class decorator with static property not working\r\n- Typescript decorators: TypeError: undefined is not a constructor\r\n- Typescript decorators not working (with es2022)\r\n- Typescript decorator es2022\r\n- Angular custom decorators not working\r\n- Angular custom decorators with aot\r\n\r\n### 🕗 Version & Regression Information\r\n\r\nRegressed when `tsconfig.compilerOptions.target` is ES2022. Other ES versions that I tried seem to be working as expected.\r\nTried Nightly version in the playground, could still repro it (after I changed target to ES2022 in TSConfig).\r\n\r\n### ⏯ Playground Link\r\n\r\n[Bug repro in Playground](https://www.typescriptlang.org/play?target=9&jsx=0&module=7&ts=5.0.0-dev.20221223#code/GYVwdgxgLglg9mABAWQKYGd0EMDmqAU0cATgFyIBi408YAlAFADeDAkBAunADaoB03ODnwADAEqocMdFGIwwOcgCIAJEyLE+YLAFtUAXyUi6Abgb6GDAAJpMuVAwjcsmRAAV5OW9jzM2ABxAAI24YCEQZLFhwgBFUYCwQbihyDwVve0QAXkQwVAB3d08MvHxTRAB6CsQAbQBBAF0A4NDwjjAZYhANfECQsMRiVCwAEwRuAE9EADUsbhBUck7PbMQlNJwlOkQmRAsLBiqi9IwffjiEpKhVvMKNkoJyo5qAISb2rl4BIXx70-s+BdEsk+LN5qhTJYjgBlfJYfz+FbySJgWBRWiIYDEOA6WqNRBQOC1N6DVBgEaoYjoAkAC2kiA4FMQ+RIAGtPHxDtVoahUDpqaFWagGZxZN1CcREPSwHBrhTgPJUCNmahuNxmXTeFKOlAsKiYOiFIgsBFddFEHodEFKcyYFA6Ug7dS4PkkE4XOhOUA)\r\nYou can try setting e.g. ES2021, and the code works, but not on ES2022.\r\n\r\n### 💻 Code\r\n\r\n`src\\index.ts`:\r\n```ts\r\nfunction Message(ctor: Function)\r\n{\r\n console.log(`Registring: \"${ctor.name}\"`);\r\n}\r\n\r\n@Message\r\nclass PingMessage\r\n{\r\n public static Default: PingMessage = new PingMessage(); // [A]\r\n public constructor(public readonly Value: string = \"Ping\") { }\r\n}\r\n\r\n// PingMessage.Default = new PingMessage(); // [B]\r\nconsole.log(PingMessage.Default.Value);\r\n\r\n// Swapping instantiation from [A] to [B] renders this code working.\r\n// Seems like constructor is not defined well while instantiating a static member within its own class.\r\n```\r\n\r\n`package.json`:\r\n```json\r\n{\r\n \"name\": \"decorator-test\",\r\n \"version\": \"1.0.0\",\r\n \"description\": \"Test App\",\r\n \"scripts\": {\r\n \"start\": \"(if exist dist rd /S /Q dist) >nul 2>nul && tsc && node ./dist/index.js\"\r\n },\r\n \"dependencies\": {\r\n \"tslib\": \"^2.4.1\"\r\n },\r\n \"devDependencies\": {\r\n \"typescript\": \"^4.9.4\"\r\n }\r\n}\r\n```\r\n\r\n`tsconfig.json`:\r\n```json\r\n{\r\n \"compileOnSave\": false,\r\n \"compilerOptions\": {\r\n \"baseUrl\": \"./\",\r\n \"outDir\": \"./dist\",\r\n \"target\": \"ES2022\", // \u003c----- !!!\r\n \"module\": \"ES2022\",\r\n \"strict\": true,\r\n \"experimentalDecorators\": true,\r\n \"useDefineForClassFields\": false,\r\n \"lib\": [\r\n \"ES2022\",\r\n \"dom\"\r\n ]\r\n },\r\n \"files\": [\r\n \"src/index.ts\"\r\n ]\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\n~\\dist\\index.js:13\r\n static { this.Default = new PingMessage_1(); } // [A]\r\n ^\r\n\r\nTypeError: undefined is not a constructor\r\n at Function.\u003cstatic_initializer> (\\~\\dist\\index.js:13:29)\r\n at Object.\u003canonymous> (\\~\\dist\\index.js:12:19)\r\n at Module._compile (node:internal/modules/cjs/loader:1155:14)\r\n at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)\r\n at Module.load (node:internal/modules/cjs/loader:1033:32)\r\n at Function.Module._load (node:internal/modules/cjs/loader:868:12)\r\n at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\r\n at node:internal/main/run_main_module:22:47\r\n\r\n### 🙂 Expected behavior\r\n\r\nConsole.logs:\r\n\r\n```\r\nRegistring: \"PingMessage\"\r\nPing\r\n```\r\n","author":{"url":"https://github.com/vladimir-siv","@type":"Person","name":"vladimir-siv"},"datePublished":"2022-12-23T18:32:58Z","url":"https://github.com/microsoft/TypeScript/issues/52004"}
| 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:c37bee00-bfd5-d3e9-18df-4d6110d03ce0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A45A:395430:BBF3B2:F9D374:6B00F0C7 |
| html-safe-nonce | 9f9e4a42ada95c571f9e1552c998546400b31b20d7f2ed54208172d4baf325e1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNDVBOjM5NTQzMDpCQkYzQjI6RjlEMzc0OjZCMDBGMEM3IiwidmlzaXRvcl9pZCI6IjI0NzM5MjgxNTgxNjA2NzkxMTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3da622a06b58c5d020c94ad8f9f4c40e537cd8e7a4bf9b342faa9ea7fe469203 |
| hovercard-subject-tag | issue:1509639007 |
| 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/52004/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e52cf16d5aabb7856a5ca578a7e7b4f1cfdced22ee85c2f8cf7527702d7a6626/microsoft/TypeScript/issues/52004 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e52cf16d5aabb7856a5ca578a7e7b4f1cfdced22ee85c2f8cf7527702d7a6626/microsoft/TypeScript/issues/52004 |
| og:image:alt | Bug Report Class decorators are not working properly when the class has a static field of the same type and is instantiating it statically. Seems like this only happens when targeting ES2022. 🔎 Sea... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vladimir-siv |
| 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