Title: Exhaustiveness checking with classes · Issue #20512 · microsoft/TypeScript · GitHub
Open Graph Title: Exhaustiveness checking with classes · Issue #20512 · microsoft/TypeScript
X Title: Exhaustiveness checking with classes · Issue #20512 · microsoft/TypeScript
Description: I'm not sure if this is a bug or a suggestion as the documentation only talks about types (as opposed to classes) and I can understand not permitting discriminated unions, also known as tagged unions or algebraic data types that involve ...
Open Graph Description: I'm not sure if this is a bug or a suggestion as the documentation only talks about types (as opposed to classes) and I can understand not permitting discriminated unions, also known as tagged unio...
X Description: I'm not sure if this is a bug or a suggestion as the documentation only talks about types (as opposed to classes) and I can understand not permitting discriminated unions, also known as tagged ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/20512
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Exhaustiveness checking with classes","articleBody":"I'm not sure if this is a bug or a suggestion as the documentation only talks about types (as opposed to classes) and I can understand not permitting discriminated unions, also known as tagged unions or algebraic data types that involve classes. I am coming from Scala which supports it, but each language is different and I guess you may be limited by Javascript. Here's some code:\r\n\r\n**TypeScript Version:** 2.6.0\r\n\r\n**Code**\r\n\r\n`Maybe.ts`\r\n\r\n```ts\r\nexport class Some\u003cA> {\r\n readonly kind: \"some\";\r\n readonly value: A;\r\n\r\n constructor(value: A) {\r\n this.value = value;\r\n }\r\n\r\n then\u003cB>(callback: (a: A) => B): Maybe\u003cB> {\r\n return new Some\u003cB>(callback(this.value)); // todo factory\r\n }\r\n}\r\n\r\nexport class None\u003cA> {\r\n readonly kind: \"none\";\r\n constructor() { }\r\n\r\n then\u003cB>(callback: (a: A) => B): Maybe\u003cB> {\r\n return new None\u003cB>();\r\n }\r\n}\r\n\r\nexport type Maybe\u003cA> = Some\u003cA> | None\u003cA>;\r\n\r\nexport function print\u003cA>(m: Maybe\u003cA>) {\r\n switch (m.kind) {\r\n case \"some\": return `Some(${m.value})`;\r\n case \"none\": return \"None\";\r\n }\r\n}\r\n```\r\n\r\n`Maybe.spec.ts`\r\n\r\n```ts\r\nimport { print, Maybe, Some, None } from \"./Maybe\";\r\nimport \"mocha\";\r\nimport { expect } from \"chai\";\r\n\r\ndescribe(\"maybe\", () => {\r\n it(\"should permit exhaustive checking\", () => {\r\n expect(print(new Some(10))).to.equal(\"Some(10)\");\r\n expect(print(new None\u003cnumber>())).to.equal(\"None\");\r\n });\r\n});\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nTest passes\r\n\r\n```\r\n maybe\r\n ✓ should permit exhaustive checking\r\n```\r\n\r\n**Actual behavior:**\r\n\r\nTest fails\r\n\r\n```\r\n 1) maybe should permit exhaustive checking:\r\n AssertionError: expected undefined to equal 'Some(10)'\r\n at Context.it (lib/Maybe.spec.js:8:63)\r\n```\r\n\r\n**Work around code**\r\n\r\n`Maybe.ts`\r\n\r\n```ts\r\nexport interface Some\u003cA> {\r\n readonly kind: \"some\";\r\n readonly value: A;\r\n}\r\n\r\nexport interface None {\r\n readonly kind: \"none\";\r\n}\r\n\r\nexport type Maybe\u003cA> = Some\u003cA> | None;\r\n\r\nexport function print\u003cA>(m: Maybe\u003cA>) {\r\n switch (m.kind) {\r\n case \"some\": return `Some(${m.value})`;\r\n case \"none\": return \"None\";\r\n }\r\n}\r\n\r\nexport function maybe\u003cA>(a: A | null | undefined): Maybe\u003cA> {\r\n if (a == null || a == undefined) {\r\n return { kind: \"none\" };\r\n } else {\r\n return { kind: \"some\", value: a};\r\n }\r\n}\r\n\r\n// todo then\u003cA, B>(a: Maybe\u003cA>, map: A => B): Maybe\u003cB>\r\n```\r\n\r\n`Maybe.spec.ts`\r\n\r\n```ts\r\nimport { print, Maybe, maybe } from \"./Maybe\";\r\nimport \"mocha\";\r\nimport { expect } from \"chai\";\r\n\r\ndescribe(\"maybe\", () => {\r\n it(\"should permit exhaustive checking\", () => {\r\n expect(print(maybe(10))).to.equal(\"Some(10)\");\r\n expect(print(maybe\u003cnumber>(null))).to.equal(\"None\");\r\n });\r\n});\r\n```\r\n\r\nThanks for the fun, manageable language.","author":{"url":"https://github.com/mattroberts297","@type":"Person","name":"mattroberts297"},"datePublished":"2017-12-06T20:10:00Z","url":"https://github.com/microsoft/TypeScript/issues/20512"}
| 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:9702ff49-83fa-0f24-ebce-1377423d081c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 88DA:E3C4F:67B907:861196:6B00E181 |
| html-safe-nonce | 2a9aa7c3f0f74c7e90784c8ebb26ccc53ef37b60f8b81e7e71f5d8db1bad4cde |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OERBOkUzQzRGOjY3QjkwNzo4NjExOTY6NkIwMEUxODEiLCJ2aXNpdG9yX2lkIjoiNjExNTk4OTkxMDM5NTYwOTQ3MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7c732f268a3ed6153e53d504859b1539f2b0f9d33da62d4915d4fdf660b03b12 |
| hovercard-subject-tag | issue:279889701 |
| 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/20512/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c1e92c3842c4cbf222acd0d20923544a47530ea76922c619c3dce1f10f337172/microsoft/TypeScript/issues/20512 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c1e92c3842c4cbf222acd0d20923544a47530ea76922c619c3dce1f10f337172/microsoft/TypeScript/issues/20512 |
| og:image:alt | I'm not sure if this is a bug or a suggestion as the documentation only talks about types (as opposed to classes) and I can understand not permitting discriminated unions, also known as tagged unio... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mattroberts297 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| 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 | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width