Title: Suggestion: Merge ambient class and interface declarations. · Issue #2961 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: Merge ambient class and interface declarations. · Issue #2961 · microsoft/TypeScript
X Title: Suggestion: Merge ambient class and interface declarations. · Issue #2961 · microsoft/TypeScript
Description: Summary To support re-opening classes and built-ins as classes, I have proposed two suggestions (#2957, #2959) to help to better re-classify built-ins as class declarations, rather than the current interface-interface-var pattern we are ...
Open Graph Description: Summary To support re-opening classes and built-ins as classes, I have proposed two suggestions (#2957, #2959) to help to better re-classify built-ins as class declarations, rather than the current...
X Description: Summary To support re-opening classes and built-ins as classes, I have proposed two suggestions (#2957, #2959) to help to better re-classify built-ins as class declarations, rather than the current...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/2961
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: Merge ambient class and interface declarations.","articleBody":"# Summary\n\nTo support re-opening classes and built-ins as classes, I have proposed two suggestions (#2957, #2959) to help to better re-classify built-ins as `class` declarations, rather than the current `interface`-`interface`-`var` pattern we are using for built-in \"classes\". However, these changes would break existing projects that currently extend built-ins by re-opening the interfaces of these built-ins. This proposal seeks to support merging these declarations, so that existing code can continue to function.\n# Current State\n\nCurrently, authors can re-open the instance or static-side interface of many built-ins in `lib.d.ts` to add new functionality. For example:\n\n``` ts\n// lib.d.ts\ninterface Array\u003cT> { /*... */ }\ninterface ArrayConstructor { /*...*/ }\ndeclare var Array: ArrayConstructor;\n\n// polyfill.ts\ninterface Array\u003cT> {\n includes(value: T): boolean;\n}\ninterface ArrayConstructor {\n of\u003cT>(...items: T[]): T[];\n}\n\nArray.prototype.includes = function (value: any) { return this.indexOf(value) != -1; }\nArray.of = function\u003cT> (...items: T[]) { return items; }\n```\n\nThe proposals in #2957 and #2959 would break existing libraries without some remediation method.\n# Proposal\n\nI propose two steps to resolve this:\n- Allow you to re-open the interface for the instance side of a class.\n- Automatically generate a named interface for the static side of an ambient class declaration in the same lexical scope.\n## Re-open instance-side\n\nRe-opening the instance-side of a class would be possible by merging interfaces with ambient class declarations in the same lexical scope:\n\n``` ts\n// lib.d.ts\ndeclare class Array\u003cT> { /* ... */ }\n\n// polyfill.ts\ninterface Array\u003cT> {\n include(value: T): boolean;\n}\n```\n# Automatic static-side interface\n\nFor backwards-compatibility, we could auto-generate a constructor interface for an ambient class declaration. As this is really intended to be a backwards-compatibility feature, we could opt to enable this only when we encounter a triple-slash directive. This would allow us to not only use this for our own `lib.d.ts`, but also allow third parties to use this directive if need to migrate their existing declarations to support these new features.\n\n``` ts\n// lib.d.ts\n/// \u003creference auto-static-interface=\"true\" />\ndeclare class Array\u003cT> { /* ... */ }\n// Creates an `ArrayConstructor` symbol for the static interface of Array\n\n// polyfill.ts\ninterface ArrayConstructor {\n of(...args: T[]): T[];\n}\n```\n","author":{"url":"https://github.com/rbuckton","@type":"Person","name":"rbuckton"},"datePublished":"2015-04-29T19:35:54Z","url":"https://github.com/microsoft/TypeScript/issues/2961"}
| 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:cabd4a5f-7126-580e-0eaf-3dd972b0ba25 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B55A:1D20B7:185349D:1EC7EFD:6B017A38 |
| html-safe-nonce | 3bc3ceb6988bf2a9c63c75178c1241daa0f113990aee6f2e08fba51c8a5543ca |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNTVBOjFEMjBCNzoxODUzNDlEOjFFQzdFRkQ6NkIwMTdBMzgiLCJ2aXNpdG9yX2lkIjoiMjc5NzY4MTA2OTYyODc2OTg1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | fea60b4994df52ae7391a2b5d89e24c56c5563e042aaec8a2789ff8bbcfdfcbd |
| hovercard-subject-tag | issue:71967928 |
| 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/2961/issue_layout |
| twitter:image | https://opengraph.githubassets.com/98ec8187122f730591032a0cbbd85b42cc06b328815944f17a0be8d4473e9087/microsoft/TypeScript/issues/2961 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/98ec8187122f730591032a0cbbd85b42cc06b328815944f17a0be8d4473e9087/microsoft/TypeScript/issues/2961 |
| og:image:alt | Summary To support re-opening classes and built-ins as classes, I have proposed two suggestions (#2957, #2959) to help to better re-classify built-ins as class declarations, rather than the current... |
| 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