Title: TS Proposal : "Interface incorrectly extends interface" - sub-interface method overload OR override ? · Issue #20920 · microsoft/TypeScript · GitHub
Open Graph Title: TS Proposal : "Interface incorrectly extends interface" - sub-interface method overload OR override ? · Issue #20920 · microsoft/TypeScript
X Title: TS Proposal : "Interface incorrectly extends interface" - sub-interface method overload OR override ? · Issue #20920 · microsoft/TypeScript
Description: TypeScript Version : 2.6.2 The problem Initial scenario I encountered the issue yesterday days as I was trying to add some features to one of my interface inheritance chain. The context of this example is just imagined : It is longer tha...
Open Graph Description: TypeScript Version : 2.6.2 The problem Initial scenario I encountered the issue yesterday days as I was trying to add some features to one of my interface inheritance chain. The context of this exa...
X Description: TypeScript Version : 2.6.2 The problem Initial scenario I encountered the issue yesterday days as I was trying to add some features to one of my interface inheritance chain. The context of this exa...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/20920
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TS Proposal : \"Interface incorrectly extends interface\" - sub-interface method overload OR override ?","articleBody":"**TypeScript Version :** 2.6.2\r\n\r\n## The problem\r\n\r\n**Initial scenario**\r\n\r\nI encountered the issue yesterday days as I was trying to add some features to one of my interface inheritance chain. The context of this example is just imagined : It is longer than needed to demonstrate the issue, but I wanted a full example to clearly show it needs to be addressed.\r\n\r\nSo, let's imagine a testing app that should instrument devices : the following is a potential model of the situation :\r\n\r\n```ts\r\ninterface Stream\r\n{\r\n}\r\ninterface InputStream\r\n{\r\n}\r\ninterface OutputStream\r\n{\r\n}\r\n// interface inheraitance is a great feature\r\ninterface IOStream extends InputStream, OutputStream\r\n{\r\n}\r\n```\r\n\r\n```ts\r\ninterface InterfaceLike\r\n{\r\n}\r\ninterface DeviceInterfaceLike extends InterfaceLike\r\n{\r\n init();\r\n}\r\ninterface InputDeviceInterfaceLike extends DeviceInterfaceLike\r\n{\r\n input(); // this should word just like the `touch` command\r\n // like if there's and error with the device, it will trigger exception\r\n input(stream: InputStream);\r\n}\r\n```\r\n\r\n```ts\r\ninterface ScreenDeviceInterfaceLike extends InputDeviceInterfaceLike\r\n{\r\n input(stream: InputStream, coordinates: {x: number, y: number});\r\n}\r\ninterface TouchScreenDeviceInterfaceLike extends InputDeviceInterfaceLike\r\n{\r\n input(stream: IOStream, coordinates: {x: number, y: number});\r\n}\r\n```\r\n\r\n**Expected behavior :**\r\n\r\nIt should just overload that method in the sub-interfaces.\r\n\r\n**Actual behavior :**\r\n\r\n```\r\nerror TS2430: Interface 'ScreenDeviceInterfaceLike' incorrectly extends interface 'InputDeviceInterfaceLike'.\r\n Types of property 'input' are incompatible.\r\n Type '(stream: InputStream, coordinates: { x: number; y: number; }) => any' is not assignable to type '{ (): any; (stream: InputStream): any; }'.\r\ncore.ts(73,11): error TS2430: Interface 'TouchScreenDeviceInterfaceLike' incorrectly extends interface 'InputDeviceInterfaceLike'.\r\n Types of property 'input' are incompatible.\r\n Type '(stream: IOStream, coordinates: { x: number; y: number; }) => any' is not assignable to type '{ (): any; (stream: InputStream): any; }'.\r\n18:38:26 - Compilation complete. Watching for file changes.\r\n```\r\n\r\nIt requires me to fully copy paste all method signatures in each interface.\r\n\r\n```ts\r\ninterface ScreenDeviceInterfaceLike extends InputDeviceInterfaceLike\r\n{\r\n input(); // this should word just like the `touch` command\r\n // like if there's and error with the device, it will trigger exception\r\n input(stream: InputStream);\r\n input(stream: InputStream, coordinates: {x: number, y: number});\r\n}\r\ninterface TouchScreenDeviceInterfaceLike extends InputDeviceInterfaceLike\r\n{\r\n input(); // this should word just like the `touch` command\r\n // like if there's and error with the device, it will trigger exception\r\n input(stream: InputStream);\r\n input(stream: IOStream, coordinates: {x: number, y: number});\r\n} \r\n```\r\n\r\nWhen you're can have up to (why not) 15 overloads distributed through inheritance chain... It becomes, (yes, you said it) bulky.\r\n\r\n-----\r\n\r\nNow it is, that the compiler cannot yet figure out when to `override` the method signatures inherited from parents and when to `overload` them.\r\n\r\n## The Proposal\r\n\r\nMy first thought was to introduce a new keyword just like `@ts-nocheck` and like.\r\n\r\n### `@override`\r\n\r\n```ts\r\ninterface InputDeviceInterfaceLike extends DeviceInterfaceLike\r\n{\r\n @override // to override all definitions from parents\r\n init(istream: InputStream);\r\n}\r\n```\r\nOR\r\n```ts\r\ninterface InputDeviceInterfaceLike extends DeviceInterfaceLike\r\n{\r\n @override\r\n {\r\n init(); // to override only this (these) definitions : forget all previous definition but this (these)\r\n }\r\n init(istream: InputStream);\r\n}\r\n```\r\n\r\nThen, the `TouchScreenDeviceInterfaceLike` interface would become :\r\n\r\n```ts\r\ninterface TouchScreenDeviceInterfaceLike extends InputDeviceInterfaceLike\r\n{\r\n @override // it is now clear that the only way to instrument a touchscreen is to provide a `IOStream` and coordinates (`{x: number, y: number}`)\r\n input(stream: IOStream, coordinates: {x: number, y: number});\r\n}\r\n```\r\n\r\n### Furthermore, `@overload`\r\n\r\nWhile the previous **`@override` is useful when the default behavior is oveloading**, we can still define the default behavior to **suppress all parent definition WHEN A METHOD IS REDEFINED in subinterface, except when `@overload` is used**. i.e\r\n\r\n```ts\r\ninterface InputDeviceInterfaceLike extends DeviceInterfaceLike\r\n{\r\n // to override all definitions from parents\r\n init(istream: InputStream);\r\n}\r\n```\r\n\r\nOR\r\n\r\n```ts\r\ninterface InputDeviceInterfaceLike extends DeviceInterfaceLike\r\n{\r\n @overload // to overload signatures from parents\r\n init(istream: InputStream);\r\n}\r\n```\r\n\r\nAnd `TouchScreenDeviceInterfaceLike` interface can become :\r\n\r\n```ts\r\ninterface TouchScreenDeviceInterfaceLike extends InputDeviceInterfaceLike\r\n{\r\n @overload // to overload signatures from parents\r\n {\r\n init(istream: InputStream); // except this signature... So that it remains only the empty parameter and the below\r\n }\r\n input(stream: IOStream, coordinates: {x: number, y: number});\r\n}\r\n```\r\n\r\n----\r\n\r\nBy thinking rigorously, I think the first step would be allow inheriting type to overload methods by default.\r\n\r\n**= = = = = = UPDATE = = = = = =**\r\nI now think it worth to add that by no mean, all through an interface inheritance chain should a method be completely wiped off (deleted, removed)... Not even by `@Override`! As this may (will) break the core concept of OOP inheritance.\r\n\r\nThe mechanism is to ensure that only some methods mood (signatures) are handled (accepted) at a given inheritance node.\r\n\r\nIn other words, if `@Override` suppresses all signatures, at least one method signature MUST be required.","author":{"url":"https://github.com/SalathielGenese","@type":"Person","name":"SalathielGenese"},"datePublished":"2017-12-28T17:45:53Z","url":"https://github.com/microsoft/TypeScript/issues/20920"}
| 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:22227f69-43d6-a373-7368-eda146e107ac |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 82E4:129351:1CF0BF1:2522143:6B03CCE0 |
| html-safe-nonce | ee4734051e5b26dba855d017b8bd8aabfd3727d762c3c2f355c4c0ef8020ca7c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MkU0OjEyOTM1MToxQ0YwQkYxOjI1MjIxNDM6NkIwM0NDRTAiLCJ2aXNpdG9yX2lkIjoiODE4NTM1OTkwNTg1MTY5MDIwOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b2cade1c7fa7b8de104c35918e1f57f42ef57d040c878ae4a8257646aba0c8ab |
| hovercard-subject-tag | issue:284960285 |
| 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/20920/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f1ee55cf912eef21c662c0ac5bb983be50630fe2229eaed081c5c8d147f9fb1d/microsoft/TypeScript/issues/20920 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f1ee55cf912eef21c662c0ac5bb983be50630fe2229eaed081c5c8d147f9fb1d/microsoft/TypeScript/issues/20920 |
| og:image:alt | TypeScript Version : 2.6.2 The problem Initial scenario I encountered the issue yesterday days as I was trying to add some features to one of my interface inheritance chain. The context of this exa... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SalathielGenese |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width