Title: Proposal: Introduce a new way to define "inherited" interfaces: likes · Issue #18762 · microsoft/TypeScript · GitHub
Open Graph Title: Proposal: Introduce a new way to define "inherited" interfaces: likes · Issue #18762 · microsoft/TypeScript
X Title: Proposal: Introduce a new way to define "inherited" interfaces: likes · Issue #18762 · microsoft/TypeScript
Description: If anyone has interest in this proposal, please let me know, I may do more future work (like try to implement it) Sometimes, we need a way to introduce an interface that is incompatible with original type but very likes to the original t...
Open Graph Description: If anyone has interest in this proposal, please let me know, I may do more future work (like try to implement it) Sometimes, we need a way to introduce an interface that is incompatible with origin...
X Description: If anyone has interest in this proposal, please let me know, I may do more future work (like try to implement it) Sometimes, we need a way to introduce an interface that is incompatible with origin...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/18762
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: Introduce a new way to define \"inherited\" interfaces: likes","articleBody":"If anyone has interest in this proposal, please let me know, I may do more future work (like try to implement it)\r\n\r\nSometimes, we need a way to introduce an interface that is incompatible with original type but very likes to the original type.\r\n\r\n\r\n\r\nFor example, in node-canvas package, they provide a class `Canvas` that very likes to the `HTMLCanvasElement`, but it **is not** compatible with `HTMLCanvasElement`. If we can have a clear way to \"clone\" an interface and make some changes, we can just use this way instead of copy-paste everything in the original type.\r\n\r\n\r\n\r\n## Proposal: Introduce a new way to define \"inherited\" interfaces: `likes`\r\n\r\n```typescript\r\n\r\ninterface A { name: string, id: number }\r\n\r\ninterface B likes A { id: typeof uuid }\r\n\r\ninterface C likes B, A extends X {}\r\n```\r\n\r\n### Changes to the grammar\r\n\r\nChange `InterfaceDeclaration` to:\r\n\r\n*interface* BindingIdentifier TypeParameters(opt) **InterfaceLikesClause(opt)** InterfaceExtendsClause (opt) ObjectType\r\n\r\n\r\nAdd `InterfaceLikesClause`:\r\n*likes* ClassOrInterfaceTypeList\r\n\r\n## Why to introduce this?\r\n1. It provides a more clear way to write two very similar (but not identical) interface.\r\n2. Developers can know what different between this interface and what it's liking. (Oh, only property `name` is incompatible with `Person`, I can treat it as a little different type of `Person`)\r\n3. If a change was made in the original type, it will automatically appear in the liking interface.\r\n\r\n## Why not to introduce this?\r\n1. This proposal introduced a new keyword `likes`\r\n2. This CFG is some bit of ambiguous about if `likes` is a variable name or a keyword.\r\n3. Maybe this scenario is not common enough to introduce a new feature to it.\r\n\r\n## When to use it\r\n1. Only a few properties are different from another one, and they have **no logically inherited relationship**.\r\n2. If you cannot modify the parent interface (like `HTMLCanvasElement`) but you really want to \"extends\" from it.\r\n\r\n## When not to use it\r\n1. Only a few properties are needed from another interface. ( Just use `ISth['name']` )\r\n2. You do not want to let other new properties automatically appear. ( Like you do not need new attributes on `HTMLElement` also appears on your `FakeElement`, you need a copy-paste )\r\n3. You can modify both `A` and `B` and they have logically inherited(or whatever) relationship. ( You should find something common, make it into `C`, and let `A` and `B` extends from `C` )\r\n4. The new interface is compatible with the original type. ( Just use `extends` )\r\n\r\n## Way to generate new type\r\n**Need to be precise**\r\n1. After we finished dealing with extends, we get interface _extended\r\n2. Do all the same things just like extends **expect** one thing: **Inherited properties with the same name must be identical (section 3.11.2).** (In typescript spec, 7.1), that means this is a conflict friendly version of interface extends.\r\nNow we get interface _extended_liked_\r\n3. Read all types in ObjectType, merge it into _extended_liked_\r\n4. We now get the final interface\r\n\r\n## Deal with confliction\r\n\r\n```typescript\r\ninterface _ likes A, B, ... extends C, D, ... { ...E }\r\n```\r\n\r\n1. If anything with the same name is not compatible in C and D, throw an Error ( Property X in C and D are not compatible )\r\n2. If anything with the same name is not compatible in A and C (or B and D, and so on), use declaration in C (or D)\r\n3.a If anything with the same name is not compatible in A and B, check if it is defined in E, if not, throw an Error ( Property X in C and D is not compatible and also not defined in the interface body, cannot determine which one to use )\r\n3.b If anything with the same name is not compatible in A and B, make this property as type `A.X | B.X`\r\n3. 3.a or 3.b, which one is better?\r\n4. If anything with the same name is not compatible in E and C, throw an Error ( Property X in the interface body are not compatible with X defined in C )\r\n5. If anything with the same name is not compatible with E and A, use that in E\r\n\r\nFor example: \r\n\r\n2.\r\n```typescript\r\ninterface Foreigner { name: string, id: number }\r\ninterface Student { id: typeof uuid }\r\ninterface ForeignerStudent likes Foreigner extends Student { }\r\n\r\n// ForeignerStudent is a subtype of Student, but not a sub-type of Foreigner\r\n// ForeignerStudent = { name: string, id: typeof uuid }\r\n```\r\n\r\n3.a\r\n```typescript\r\ninterface Foreigner { name: string, id: number }\r\ninterface Student { id: typeof uuid }\r\ninterface ForeignerStudent likes Foreigner, Student { }\r\n// Error: Property id is not compatible\r\n// typeof uuid is not compatible with number\r\n// You need to specify a type for id\r\ninterface ForeignerStudent2 likes Foreigner, Student { id: typeof uuid }\r\n// This is fine.\r\n```\r\n\r\n3.b\r\n```typescript\r\ninterface Foreigner { name: string, id: number }\r\ninterface Student { id: typeof uuid }\r\ninterface ForeignerStudent likes Foreigner, Student { }\r\n// ForeignerStudent = { name: string, id: number | typeof uuid }\r\n```\r\n\r\n## Others\r\n\r\n**What if I want to remove a property inherited by `likes`?**\r\nI have 2 ideas at present\r\n```typescript\r\ninterface N likes M {\r\n y: never // this way\r\n delete x // or this way\r\n}\r\n```\r\nIf anyone has interest in this proposal, I will consider it later.\r\n\r\n**When will a liked interface compatible with the original interface?**\r\nJust treat it as an another identical normal interface.\r\n\r\n**Does it breaks the type system?**\r\nI don't think so. It is *not* a subtype of the original type.\r\n\r\n**What about generics work with proposal?**\r\nNo idea, I will think it later.","author":{"url":"https://github.com/Jack-Works","@type":"Person","name":"Jack-Works"},"datePublished":"2017-09-26T09:31:29Z","url":"https://github.com/microsoft/TypeScript/issues/18762"}
| 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:d5651603-3017-c88b-76cd-5ab5a38152ee |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9ABA:C2718:6B0FBB:8C7BA5:6B00D482 |
| html-safe-nonce | 0023d7d41fa02a9a47fd45bb74b72825484fd5720539b9e4942ecb5b266b2957 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QUJBOkMyNzE4OjZCMEZCQjo4QzdCQTU6NkIwMEQ0ODIiLCJ2aXNpdG9yX2lkIjoiNDU0ODQ3Njc2NzQ4NDA0MDMyMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1ea66052360300383473973278e0e4ae0579efa95c6244e266a45aaaa37b534d |
| hovercard-subject-tag | issue:260555330 |
| 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/18762/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7f33baaf23a3f33f45eb40ae46b8d05a3b86580dca252437a35b6f3211afb2c2/microsoft/TypeScript/issues/18762 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7f33baaf23a3f33f45eb40ae46b8d05a3b86580dca252437a35b6f3211afb2c2/microsoft/TypeScript/issues/18762 |
| og:image:alt | If anyone has interest in this proposal, please let me know, I may do more future work (like try to implement it) Sometimes, we need a way to introduce an interface that is incompatible with origin... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Jack-Works |
| hostname | github.com |
| expected-hostname | github.com |
| None | 85bbdfe2b6ba16e234513bc6ceff5d848ef8449820c900692a2b4e67061931e5 |
| 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 | 4943239a6d2c591325dad6e5c313e1cafda877f6 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width