Title: Ability to decorate abstract member function · Issue #37067 · microsoft/TypeScript · GitHub
Open Graph Title: Ability to decorate abstract member function · Issue #37067 · microsoft/TypeScript
X Title: Ability to decorate abstract member function · Issue #37067 · microsoft/TypeScript
Description: Search Terms I search "decorate abstract member" in the issues list, and #20887 has memtioned this issue but closed. Suggestion Ability to decorate abstract function and its params. Use Cases To implement a retrofit-like http request lib...
Open Graph Description: Search Terms I search "decorate abstract member" in the issues list, and #20887 has memtioned this issue but closed. Suggestion Ability to decorate abstract function and its params. Use Cases To im...
X Description: Search Terms I search "decorate abstract member" in the issues list, and #20887 has memtioned this issue but closed. Suggestion Ability to decorate abstract function and its params. Use C...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/37067
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Ability to decorate abstract member function","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker.\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ, especially the \"Common Feature Requests\" section: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\n-->\r\n\r\n## Search Terms\r\n\r\nI search \"decorate abstract member\" in the issues list, and #20887 has memtioned this issue but closed.\r\n\r\n## Suggestion\r\n\r\n\u003c!-- A summary of what you'd like to see added or changed -->\r\nAbility to decorate abstract function and its params.\r\n\r\n## Use Cases\r\nTo implement a [retrofit](https://square.github.io/retrofit/)-like http request library with Typescript. Examples from retrofit website: \r\n```java\r\npublic interface GitHubService {\r\n @GET(\"users/{user}/repos\")\r\n Call\u003cList\u003cRepo>> listRepos(@Path(\"user\") String user);\r\n}\r\n```\r\nWhen translate it into Typescript, we need to decorate an abstract class since interface will be erased after compilation. But,\r\n```typescript\r\n@MyAPI() // Okay, abstract class is decoratable\r\nabstract class GitHubService {\r\n @GET(\"users/{user}/repos\") // ERROR: Cannot decorate an abstract class member\r\n abstract Call\u003cList\u003cRepo>> listRepos(@Path(\"user\") user: string); // ERROR \r\n}\r\n```\r\n\u003c!--\r\nWhat do you want to use this for?\r\nWhat shortcomings exist with current approaches?\r\n-->\r\nTo workaround such limitation, we cannot use abstract class, so it turns out to be\r\n```typescript\r\n@MyAPI() // Okay, abstract class is decoratable\r\nclass GitHubService {\r\n @GET(\"users/{user}/repos\") \r\n Call\u003cList\u003cRepo>> listRepos(@Path(\"user\") user: user) { \r\n throw new Error('unimplemented');\r\n } \r\n}\r\n```\r\nThis is obviousely not elegant.\r\n\r\nI think such ability can be implemented without breaks existing valid code. \r\nDecorator function can determin wheter it's decorating an abstract member by check if value of property descriptor is undefined.\r\n\r\n```typescript\r\nfunction ClassMemberDecorator(prototype: {}, name: string, pd: PropertyDescriptor) {\r\n if (typeof pd.value === 'undefined') { // we're decorating abstract member\r\n }\r\n}\r\n\r\nfunction ClassMemberParamDecorator(prototype: {}, name: string, index: number) {\r\n if (typeof prototype[name] === 'undefiend') { // we're decorating abstract member param\r\n }\r\n}\r\n```\r\nSince when targetting ES3, `PropertyDescriptor.value` is always undefined, this feature shall only be supported when targetting ES5-onward. \r\n\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n","author":{"url":"https://github.com/thynson","@type":"Person","name":"thynson"},"datePublished":"2020-02-27T03:21:04Z","url":"https://github.com/microsoft/TypeScript/issues/37067"}
| 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:58e737fc-e6ac-3357-565a-f879213046f0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B022:27D3AD:6BEF7E:8ADAAE:6B00E138 |
| html-safe-nonce | 3d1292dcebf0be47220675fbc7d71991a79cf03a12343a773c86b2f9d8e5f1c5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMDIyOjI3RDNBRDo2QkVGN0U6OEFEQUFFOjZCMDBFMTM4IiwidmlzaXRvcl9pZCI6IjcxMjcyNjY3MDM5NzcyNzAzMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2afe339cf952595389c1329437cef144a80a5c049cb5cac5143ed56f527d13bf |
| hovercard-subject-tag | issue:571794127 |
| 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/37067/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1ff7e23b89b67bcf31e5bdff81dc31048104123ebd2fa5d8e5371d5006023653/microsoft/TypeScript/issues/37067 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1ff7e23b89b67bcf31e5bdff81dc31048104123ebd2fa5d8e5371d5006023653/microsoft/TypeScript/issues/37067 |
| og:image:alt | Search Terms I search "decorate abstract member" in the issues list, and #20887 has memtioned this issue but closed. Suggestion Ability to decorate abstract function and its params. Use Cases To im... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | thynson |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| 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 | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width