Title: ADTs with inheritance · Issue #20144 · microsoft/TypeScript · GitHub
Open Graph Title: ADTs with inheritance · Issue #20144 · microsoft/TypeScript
X Title: ADTs with inheritance · Issue #20144 · microsoft/TypeScript
Description: I'd like to define an ADT and also enforce that all concrete types implement another interface (I know that is not how ADTs are supposed to be used, but since TypeScript is a mixed paradigm language, I'd like to be able to do it the way ...
Open Graph Description: I'd like to define an ADT and also enforce that all concrete types implement another interface (I know that is not how ADTs are supposed to be used, but since TypeScript is a mixed paradigm languag...
X Description: I'd like to define an ADT and also enforce that all concrete types implement another interface (I know that is not how ADTs are supposed to be used, but since TypeScript is a mixed paradigm lan...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/20144
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ADTs with inheritance","articleBody":"I'd like to define an ADT and also enforce that all concrete types implement another interface (I know that is not how ADTs are supposed to be used, but since TypeScript is a mixed paradigm language, I'd like to be able to do it the way I'm used to it in Scala). This is an attempt to achieve it:\r\n\r\n```typescript\r\ntype Shape = Circle | Rectangle\r\n\r\ninterface Drawable {\r\n draw();\r\n}\r\n\r\nclass Circle implements Drawable {\r\n constructor(readonly kind: \"circle\", readonly radius: number) { }\r\n draw() {}\r\n}\r\n\r\nclass Rectangle implements Drawable {\r\n constructor(readonly kind: \"square\", readonly weight: number, readonly height: number) { }\r\n draw() {} \r\n}\r\n\r\nlet s: Shape;\r\ns.draw();\r\n```\r\n\r\nThe problem is that I cannot enforce that all concrete classes implement `Drawable` when defining them:\r\n\r\n```typescript\r\ntype Shape = Circle | Square | Triangle\r\n\r\n// This is ok, since there is no way to enforce Triangle to implement Drawable\r\nclass Triangle {\r\n constructor(readonly kind: \"square\", readonly side1: number, readonly side2: number, side3: number) { }\r\n}\r\n\r\nlet s: Shape;\r\ns.draw(); // Error: property draw does not exist on Shape\r\n```\r\n\r\nand I only will get an error when actually call `draw()` on a `Shape` instance.\r\n\r\nIn Scala, for example, I can enforce that all concrete case classes to implement the `Drawable` interface:\r\n\r\n```scala\r\ntrait Drawable {\r\n def draw(): Unit\r\n}\r\n\r\ntrait Shape extends Drawable\r\n\r\ncase class Circle(radius: Int) extends Shape {\r\n override def draw() {}\r\n}\r\n\r\ncase class Square(width: Int, height: Int) extends Shape {\r\n override def draw() {}\r\n}\r\n\r\n// Error: Triangle does not implement draw\r\ncase class Triangle(side1: Int, side2: Int, side3: Int) extends Shape {\r\n}\r\n```\r\n\r\nIs there a way to achieve this in TypeScript?\r\nIs there a plan to allow discriminated union type matching feature with interfaces?\r\nIs there a plan to allow type aliases to extend interfaces?","author":{"url":"https://github.com/afroozeh","@type":"Person","name":"afroozeh"},"datePublished":"2017-11-19T22:01:07Z","url":"https://github.com/microsoft/TypeScript/issues/20144"}
| 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:243a9b5e-0049-a300-a9ff-e76406a54c40 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 948A:3923DE:D8757F:1290148:6AFBED0C |
| html-safe-nonce | 5ac6fddc023bdfca9f5d999d6045fa49a0390429170d3a3c2d4d8f11b00a0854 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NDhBOjM5MjNERTpEODc1N0Y6MTI5MDE0ODo2QUZCRUQwQyIsInZpc2l0b3JfaWQiOiI0MDMzNjkwNDg3MTI1NDM3NzA4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 74f3e3071e51441297329061ee3f4dc5553dcbd57a9a279801ded82a3fb688b6 |
| hovercard-subject-tag | issue:275196674 |
| 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/20144/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5f989d8d8421c2282ea77dcf018a9cc937d4b5ea00a66211dda81189ae9bcbec/microsoft/TypeScript/issues/20144 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5f989d8d8421c2282ea77dcf018a9cc937d4b5ea00a66211dda81189ae9bcbec/microsoft/TypeScript/issues/20144 |
| og:image:alt | I'd like to define an ADT and also enforce that all concrete types implement another interface (I know that is not how ADTs are supposed to be used, but since TypeScript is a mixed paradigm languag... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | afroozeh |
| hostname | github.com |
| expected-hostname | github.com |
| None | 744d4381b1c4dbd6d8736c7d7e5bbe7dc11217584e457a3555020c631764e8ec |
| 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 | df859c48a4c8b4c0009c19bb4e82101aacb4b998 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width