Title: Class is anonymous for decorator code · Issue #6420 · microsoft/TypeScript · GitHub
Open Graph Title: Class is anonymous for decorator code · Issue #6420 · microsoft/TypeScript
X Title: Class is anonymous for decorator code · Issue #6420 · microsoft/TypeScript
Description: As an Javascript developer, I tried to implement the dependency injection, by using the tool introduced in rbuckton/ReflectDecorators (https://github.com/rbuckton/ReflectDecorators.git). However, I just found the following code will be c...
Open Graph Description: As an Javascript developer, I tried to implement the dependency injection, by using the tool introduced in rbuckton/ReflectDecorators (https://github.com/rbuckton/ReflectDecorators.git). However, I...
X Description: As an Javascript developer, I tried to implement the dependency injection, by using the tool introduced in rbuckton/ReflectDecorators (https://github.com/rbuckton/ReflectDecorators.git). However, I...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/6420
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Class is anonymous for decorator code","articleBody":"As an Javascript developer, I tried to implement the dependency injection, by using the tool introduced in rbuckton/ReflectDecorators (https://github.com/rbuckton/ReflectDecorators.git). However, I just found the following code will be compiled into an anonymous class declaration in js file.\n\n``` typescript\n @log\n class A {\n constructor() {\n Debug.log('A created');\n }\n\n afunc() {}\n\n bfunc() {}\n\n }\n```\n\nThe decorator @log is quite simple, it is to print the class name.\n\n``` typescript\n// class decorator\nfunction log(target: any) {\n console.log(target.name);\n ....\n}\n```\n\nThe generated javascript is as follows. I turned \"emitDecoratorMetadata\" to true in tsconfig.json\n\n``` javascript\n let A = class {\n constructor() {\n Debug_1.Debug.log('A created');\n }\n afunc() { }\n bfunc() { }\n };\n A = __decorate([\n decorators_1.log, \n __metadata('design:paramtypes', [])\n ], A);\n```\n\nI am so frustrated to see that constructor A is actually a class without name - the target in decorator code is an actually anonymous constructor function.\n\nI do not know why. I can not figure out why make it anonymous if one decorator is there. As a result, I have to implement it like the following, which looks really odd and confusing, by using metadata Reflect API:\n\n``` typescript\n @logClass('A')\n class A {\n constructor() {\n Debug.log('A created');\n }\n\n afunc() {}\n\n bfunc() {}\n\n }\n```\n\nAfter investigation in sources of Typescript, I found the following lines related to this issue:\nin function emitClassLikeDeclarationForES6AndHigher(node), it reads:\n\n``` javascript\n ...\n write(\"class\");\n if ((node.name || (node.flags & 1024 && staticProperties.length > 0)) && !thisNodeIsDecorated) {\n write(\" \");\n emitDeclarationName(node);\n }\n```\n\nSo while compiling class A, thisNodeIsDecorated is true and the name is never emitted. May I ask why? Would it better to just emit class name, like the following? Please let me know, if I missed something. Thanks very much.\n\n``` javascript\n let A = class A {\n constructor() {\n Debug_1.Debug.log('A created');\n }\n afunc() { }\n bfunc() { }\n };\n A = __decorate([\n decorators_1.log, \n __metadata('design:paramtypes', [])\n ], A);\n\n```\n","author":{"url":"https://github.com/TheLevenCreations","@type":"Person","name":"TheLevenCreations"},"datePublished":"2016-01-09T12:21:21Z","url":"https://github.com/microsoft/TypeScript/issues/6420"}
| 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:ec0b9352-225c-8de8-5df0-ec2a516c522b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E174:6DC60:2EEEBF:3AC23C:6B0101D3 |
| html-safe-nonce | e7c10d378e51702641f1effec110fbf4ec8cef490ca6e160c6ff087b029e0efd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMTc0OjZEQzYwOjJFRUVCRjozQUMyM0M6NkIwMTAxRDMiLCJ2aXNpdG9yX2lkIjoiMzIyMzMxNzY2ODMyOTIyNjcwNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 97d58a235bf04decc774e70eff7a6873729a898b976450138118f0c3d29fd0e1 |
| hovercard-subject-tag | issue:125755718 |
| 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/6420/issue_layout |
| twitter:image | https://opengraph.githubassets.com/443050bd24f4c6e208194612c13e3efd7988c290950cc7eda5ef7386c6380151/microsoft/TypeScript/issues/6420 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/443050bd24f4c6e208194612c13e3efd7988c290950cc7eda5ef7386c6380151/microsoft/TypeScript/issues/6420 |
| og:image:alt | As an Javascript developer, I tried to implement the dependency injection, by using the tool introduced in rbuckton/ReflectDecorators (https://github.com/rbuckton/ReflectDecorators.git). However, I... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | TheLevenCreations |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width