Title: Decorators · Issue #2249 · microsoft/TypeScript · GitHub
Open Graph Title: Decorators · Issue #2249 · microsoft/TypeScript
X Title: Decorators · Issue #2249 · microsoft/TypeScript
Description: ES7 proposal The ES7 proposal for decorators can be found here: https://github.com/wycats/javascript-decorators The ES7 proposal serves as the base of this proposal. Below are notes about how the type system Decorator targets: Class cons...
Open Graph Description: ES7 proposal The ES7 proposal for decorators can be found here: https://github.com/wycats/javascript-decorators The ES7 proposal serves as the base of this proposal. Below are notes about how the t...
X Description: ES7 proposal The ES7 proposal for decorators can be found here: https://github.com/wycats/javascript-decorators The ES7 proposal serves as the base of this proposal. Below are notes about how the t...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/2249
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Decorators","articleBody":"\u003e ## ES7 proposal\n\u003e \n\u003e The ES7 proposal for decorators can be found here: https://github.com/wycats/javascript-decorators\n\u003e The ES7 proposal serves as the base of this proposal. Below are notes about how the type system \n## Decorator targets:\n### Class constructor\n\n``` ts\n@F(\"color\")\n@G\nclass Foo {\n}\n```\n\ndesugars to:\n\n``` js\nvar Foo = (function () {\n function Foo() {\n }\n Foo = __decorate([F(\"color\"), G], Foo);\n return Foo;\n})();\n```\n### Methods\n\n``` ts\nclass Foo {\n @F(color)\n @G\n bar() { }\n}\n```\n\ndesugars to:\n\n``` js\nvar Foo = (function () {\n function Foo() {\n }\n Foo.prototype.bar = function () {\n };\n Object.defineProperty(Foo.prototype, \"bar\", __decorate([F(color), G], Foo.prototype, \"bar\", Object.getOwnPropertyDescriptor(Foo.prototype, \"bar\")));\n return Foo;\n})();\n```\n## Static method\n\n``` ts\nclass Foo {\n @F(\"color\")\n @G\n static sMethod() {}\n}\n```\n\ndesugars to:\n\n``` js\nvar Foo = (function () {\n function Foo() {\n }\n Foo.sMethod = function () {\n };\n Object.defineProperty(Foo, \"sMethod\", __decorate([F(\"color\"), G], Foo, \"sMethod\", Object.getOwnPropertyDescriptor(Foo, \"sMethod\")));\n return Foo;\n})();\n```\n### Properties\n\n``` ts\nclass Foo {\n @F(\"color\")\n @G\n prop: number;\n}\n```\n\ndesugars to:\n\n``` js\nvar Foo = (function () {\n function Foo() {\n }\n __decorate([F(\"color\"), G], Foo.prototype, \"prop\");\n return Foo;\n})();\n```\n### Method/Accessor formal parameter\n\n``` ts\nclass Foo {\n method(@G a, @F(\"color\") b) {}\n}\n```\n\ndesugars to:\n\n``` js\nvar Foo = (function () {\n function Foo() {\n }\n Foo.prototype.method = function (a, b) {\n };\n __decorate([G], Foo.prototype, \"method\", 0);\n __decorate([F(\"color\")], Foo.prototype, \"method\", 1);\n return Foo;\n})();\n```\n\nWhere the __decorate is defined as:\n\n``` js\nvar __decorate = this.__decorate || function (decorators, target, key, value) {\n var kind = typeof (arguments.length == 2 ? value = target : value);\n for (var i = decorators.length - 1; i \u003e= 0; --i) {\n var decorator = decorators[i];\n switch (kind) {\n case \"function\": value = decorator(value) || value; break;\n case \"number\": decorator(target, key, value); break;\n case \"undefined\": decorator(target, key); break;\n case \"object\": value = decorator(target, key, value) || value; break;\n }\n }\n return value;\n};\n```\n## Decorator signatures:\n\nA valid decorator should be:\n1. Assignable to one of the Decorator types (ClassDecorator | PropertyDecorator | MethodDecorator | ParameterDecorator) as described below.\n2. Return a value (in the case of class decorators and method decorator) that is assignable to the decorated value.\n\n``` ts\ndeclare type ClassDecorator = \u003cTFunction extends Function\u003e(target: TFunction) =\u003e TFunction | void;\ndeclare type PropertyDecorator = (target: Object, propertyKey: string | symbol) =\u003e void;\ndeclare type MethodDecorator = \u003cT\u003e(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor\u003cT\u003e) =\u003e TypedPropertyDescriptor\u003cT\u003e | void;\ndeclare type ParameterDecorator = (target: Function, propertyKey: string | symbol, parameterIndex: number) =\u003e void;\n```\n### Notes:\n- Decorating a function declaration is not allowed as it will block hoisting the function to the top of the scope, which is a significant change in semantics. \n- Decorating function expressions and arrow functions are not supported. The same effect can be achived by applying the decorator function as `var x = dec(function () { });` \n- Decorating function formal parameters is currently not part of the ES7 proposal.\n- Decorators are not allowed when targeting ES3\n","author":{"url":"https://github.com/rbuckton","@type":"Person","name":"rbuckton"},"datePublished":"2015-03-07T03:14:53.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":139},"url":"https://github.com/2249/TypeScript/issues/2249"}
| 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:61616ad7-07c0-4544-2adb-00a7c42e6650 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DB6C:1BE754:2DBD53:3FDFDF:6A62F33B |
| html-safe-nonce | 711f02e6e1be7cfd890ede2574c28075812d421735fbc5cc15e3c7f3aa97794a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQjZDOjFCRTc1NDoyREJENTM6M0ZERkRGOjZBNjJGMzNCIiwidmlzaXRvcl9pZCI6IjE2MDc0MTUxODE4Nzc2Mzc5NDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f21f0b80eedabc87fb1a2474e7dfc587c3aa16c2246642c51721c5dbb35b1d4c |
| hovercard-subject-tag | issue:60187635 |
| 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/2249/issue_layout |
| twitter:image | https://opengraph.githubassets.com/02c1ddad5e12108591d64e0e0f9429e6a24d85dc7dd76955211d106f2220a2a9/microsoft/TypeScript/issues/2249 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/02c1ddad5e12108591d64e0e0f9429e6a24d85dc7dd76955211d106f2220a2a9/microsoft/TypeScript/issues/2249 |
| og:image:alt | ES7 proposal The ES7 proposal for decorators can be found here: https://github.com/wycats/javascript-decorators The ES7 proposal serves as the base of this proposal. Below are notes about how the t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rbuckton |
| hostname | github.com |
| expected-hostname | github.com |
| None | b415018e190e73858133ddcaa36acce7b3f3572fe54dda84bd3b21a6ec714c30 |
| 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 | 22f98521e99f504294ab0812b66104d50eb75a70 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width