Title: Suggestion: Reopen static and instance side of classes · Issue #2957 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: Reopen static and instance side of classes · Issue #2957 · microsoft/TypeScript
X Title: Suggestion: Reopen static and instance side of classes · Issue #2957 · microsoft/TypeScript
Description: Summary To support both the semantics of subclassing built-ins in ES6 and still allow authors to augment built-ins, we need a mechanism to reopen the static and instance sides of a class. Current state Today we can re-open interfaces, al...
Open Graph Description: Summary To support both the semantics of subclassing built-ins in ES6 and still allow authors to augment built-ins, we need a mechanism to reopen the static and instance sides of a class. Current s...
X Description: Summary To support both the semantics of subclassing built-ins in ES6 and still allow authors to augment built-ins, we need a mechanism to reopen the static and instance sides of a class. Current s...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/2957
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: Reopen static and instance side of classes","articleBody":"# Summary\n\nTo support both the semantics of subclassing built-ins in ES6 and still allow authors to augment built-ins, we need a mechanism to reopen the static and instance sides of a class. \n# Current state\n\nToday we can re-open interfaces, allowing authors to augment built-ins (for example, to support polyfills):\n\n``` ts\n// in lib.d.ts\ninterface Array\u003cT> { /*...*/ }\ninterface ArrayConstructor { /*...*/ }\ndeclare var Array: ArrayConstructor;\n\n// in polyfill.ts\ninterface Array\u003cT> {\n includes(value: T): Boolean;\n}\ninterface ArrayConstructor {\n of\u003cT>(...items: T[]): Array\u003cT>;\n}\n\nArray.prototype.includes = function (value: any) { return this.indexOf(value) != -1; }\nArray.of = function\u003cT> (...items: T[]) { return items; }\n```\n\nWe can also re-open the static side of a class, in a limited fashion:\n\n``` ts\n// initial declaration\nclass MyClass {\n}\n\n// re-open\nmodule MyClass {\n export var staticProperty = 1;\n}\n```\n\nThere are several issues with these approaches:\n- You cannot use type defined by the `var`/`interface` pattern in the `extends` clause of a class in TypeScript, meaning that \"classes\" defined using this pattern cannot be subclassed in ES6, which is an issue for built-ins.\n- While you can re-open the static side of a class using `module`, you can only use non-keyword identifiers for property names. So you could not, for example, add a `[Symbol.species]` property to the class, or use decorators on these members.\n- There is no way to re-open the instance side of a class.\n# Proposal\n\nI propose we add a new syntactic modifier for the `class` declaration that would indicate we are re-opening an existing class. For this example I am using the keyword `partial`, although the semantics here differ significantly than the same-named capability in C#:\n\n``` ts\n// in lib.d.ts\ndeclare class Array\u003cT> {\n}\n\n// in polyfill.ts\npartial class Array\u003cT> {\n static of\u003cT>(...items: T[]) { return items; }\n includes(value: T): boolean { return this.indexOf(value) != -1; }\n}\n\n// emit (ES5)\nArray.of = function() { \n var items = [];\n for (var _i = 0; i \u003c arguments.length; i++) \n items[i] = arguments[i];\n return items;\n}\nArray.prototype.includes = function(value) {\n return this.indexOf(value) != -1;\n}\n```\n## Rules\n- A `partial` class declaration must be preceded by a non-`partial` class declaration in the same lexical scope. These should be the same rules that apply when merging a module with a class or function today.\n- A `partial` class declaration must have the same module visibility as the preceding non-`partial` class declaration.\n- A `partial` class declaration must have the same generic type parameters (including constraints) as the non-`partial` class declaration.\n- A `partial` class declaration cannot have an `extends` clause, but may have an `implements` clause.\n- A `partial` class declaration cannot have a `constructor` member.\n- A `partial` class declaration cannot have members with the same name as existing members on a class.\n - Exception: ambient partial class declaration members can merge with other ambient partial class declaration members if they are compatible overloads, similar to interfaces.\n- Non-static property declarations on a `partial` class declaration cannot have initializers.\n- A `partial` class declaration can have a class decorator. User code that executes in-between the initial class declaration and the partial declaration will be able to observe the class before decorators on the `partial` class are applied.\n - NOTE: We could choose to disallow class decorators on a `partial` class.\n# Out of scope\n- This proposal does not cover the case where built-in \"classes\" can often also be called as functions. This case is covered in #2959.\n- This proposal does not cover the case where authors may have already extended the interface of a built in. This case is covered in #2961.\n# Previous discussions\n\nThis has also been discussed previously:\n- #9 - Suggestion: Extension methods\n- #563 - Partial classes\n- https://typescript.codeplex.com/workitem/100\n","author":{"url":"https://github.com/rbuckton","@type":"Person","name":"rbuckton"},"datePublished":"2015-04-29T18:39:09Z","url":"https://github.com/microsoft/TypeScript/issues/2957"}
| 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:fb401b05-d135-a1f8-afe6-c9220ecafc3e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DDA4:3E4205:2110B87:2C325BE:6B018A67 |
| html-safe-nonce | 24548aed27d49d54fee9638a90bec827e2e2f45d35a7d2f240d21ae35171dcd3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEREE0OjNFNDIwNToyMTEwQjg3OjJDMzI1QkU6NkIwMThBNjciLCJ2aXNpdG9yX2lkIjoiOTA1NzU0NjkyMDQ2OTMwMTg2MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 142b02e43a84463c0ea97ff4ea862cb2bb058159475354d9ad96b1cccd3ab8a4 |
| hovercard-subject-tag | issue:71954116 |
| 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/2957/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0a12b4e57d833c073c87b13a4be8b691993f597d7fccac2db8ade50c115ee5ab/microsoft/TypeScript/issues/2957 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0a12b4e57d833c073c87b13a4be8b691993f597d7fccac2db8ade50c115ee5ab/microsoft/TypeScript/issues/2957 |
| og:image:alt | Summary To support both the semantics of subclassing built-ins in ES6 and still allow authors to augment built-ins, we need a mechanism to reopen the static and instance sides of a class. Current s... |
| 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 | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width