Title: Proposal: structural overloading for interfaces · Issue #4302 · microsoft/TypeScript · GitHub
Open Graph Title: Proposal: structural overloading for interfaces · Issue #4302 · microsoft/TypeScript
X Title: Proposal: structural overloading for interfaces · Issue #4302 · microsoft/TypeScript
Description: An alternative to extends that is mostly identical but with the following variation: instead of erroring when properties are re-declared with different types, it would resolve to the type unions instead (properties having a function type...
Open Graph Description: An alternative to extends that is mostly identical but with the following variation: instead of erroring when properties are re-declared with different types, it would resolve to the type unions in...
X Description: An alternative to extends that is mostly identical but with the following variation: instead of erroring when properties are re-declared with different types, it would resolve to the type unions in...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/4302
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: structural overloading for interfaces","articleBody":"An alternative to `extends` that is mostly identical but with the following variation: instead of erroring when properties are re-declared with different types, it would resolve to the type unions instead (properties having a function type are already overloaded in inheritance, so there's no need to address them):\n\n``` ts\ninterface A {\n x: boolean;\n}\n\ninterface B extends A { // Error! Incorrectly extends A. Types of x are incompatible.\n x: number;\n}\n\ninterface C overloads A { // OK! x is now of type boolean | number\n x: number;\n}\n\ninterface D overloads A { // OK! x is now of type boolean | number | string\n x: number | string;\n}\n\ninterface E overloads A { // OK! x is now of type boolean | (param: number): string\n x(param: number): string;\n}\n```\n\nAn operator based on this variation of inheritance would have the property that for any two (object or function) interfaces `A`, `B`, `A overload B` (illustrative, not suggested, name) can never fail, even if the operands contain incompatible property types (note: this assumes cycles are ignored in the operator version). In contrast to intersection, it wouldn't even fail if `A` is an object type and `B` is a function type, as hybrid object/function types are already allowed by standard inheritance. \n\nThis an attempt to address the [challenge presented by @ahejlsberg](https://github.com/Microsoft/TypeScript/issues/4278#issuecomment-130240112) of having an inheritance-like operation that is guaranteed not to fail when applied to generic type parameters:\n\n``` ts\ndeclare function combine\u003cT, U>(obj1: T, obj2: U): T overload U;\n```\n\nand thus may be used as a replacement to intersection that would be more consistent with the rest of the language (and inheritance logic in particular).\n\nEven though I'm not a computer scientist or a language expert I tried to do my best for giving a basic direction (that is, at least a possible idea) on how to solve this. I'm not in a position to be able to see all the details, nuances or problems for integrating something like this into the language.\n","author":{"url":"https://github.com/rotemdan","@type":"Person","name":"rotemdan"},"datePublished":"2015-08-13T17:44:46Z","url":"https://github.com/microsoft/TypeScript/issues/4302"}
| 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:3fa07f2b-5bc8-8a92-3c3e-38acefc2c69c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ED86:3DF0BE:786B91:9E382F:6B00D99F |
| html-safe-nonce | a0c9763c17353d4470f3b123334f3e0645a81f63d43f164d7a3105a2a3e39948 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDg2OjNERjBCRTo3ODZCOTE6OUUzODJGOjZCMDBEOTlGIiwidmlzaXRvcl9pZCI6IjczODcxOTUxMTgyNTQwNjM2NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1c4890efea4c375a0b09a54649cdc2bf2c4a952510242689e53a3abb6dec8090 |
| hovercard-subject-tag | issue:100827909 |
| 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/4302/issue_layout |
| twitter:image | https://opengraph.githubassets.com/174c4db2247c75b99f5139e849b3b9f0edeca34c7bc9b5a7ca38d67bf8438485/microsoft/TypeScript/issues/4302 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/174c4db2247c75b99f5139e849b3b9f0edeca34c7bc9b5a7ca38d67bf8438485/microsoft/TypeScript/issues/4302 |
| og:image:alt | An alternative to extends that is mostly identical but with the following variation: instead of erroring when properties are re-declared with different types, it would resolve to the type unions in... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rotemdan |
| hostname | github.com |
| expected-hostname | github.com |
| None | a481f62a1e04c9b9bdcb5628230682c66d59006f5b13b080f79cba84a176905b |
| 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 | 84c8c5cc25e2fb7f2308428458538436baecc6d2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width