Title: Confusing error when returning generic in terms of `this` from method called on union of subclasses · Issue #36307 · microsoft/TypeScript · GitHub
Open Graph Title: Confusing error when returning generic in terms of `this` from method called on union of subclasses · Issue #36307 · microsoft/TypeScript
X Title: Confusing error when returning generic in terms of `this` from method called on union of subclasses · Issue #36307 · microsoft/TypeScript
Description: TypeScript Version: 3.8.0-dev.20200118 Search Terms: union of subclasses with "this" generic Code declare class Foo { doThing(): Promise
Open Graph Description: TypeScript Version: 3.8.0-dev.20200118 Search Terms: union of subclasses with "this" generic Code declare class Foo { doThing(): Promise
X Description: TypeScript Version: 3.8.0-dev.20200118 Search Terms: union of subclasses with "this" generic Code declare class Foo { doThing(): Promise<this> } declare class Bar extends Foo { bar:...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/36307
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Confusing error when returning generic in terms of `this` from method called on union of subclasses","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!--\r\nPlease try to reproduce the issue with the latest published version. It may have already been fixed.\r\nFor npm: `typescript@next`\r\nThis is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly\r\n-->\r\n**TypeScript Version:** 3.8.0-dev.20200118\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** union of subclasses with \"this\" generic\r\n\r\n**Code**\r\n\r\n```ts\r\ndeclare class Foo {\r\n doThing(): Promise\u003cthis>\r\n}\r\n\r\ndeclare class Bar extends Foo {\r\n bar: number;\r\n}\r\ndeclare class Baz extends Foo {\r\n baz: number;\r\n}\r\n\r\ndeclare var a: Bar | Baz;\r\na.doThing().then(result => {\r\n\t// whatever\r\n});\r\n```\r\n\r\n[Playground Link](https://www.typescriptlang.org/play/?ts=3.8.0-dev.20200118&ssl=1&ssc=1&pln=16&pc=1#code/CYUwxgNghgTiAEkoGdnwGIHtPwN4Ch4j5hMAVACwEsA7AcwAoBKALngAUZMBbK5EADwAXasgB8+AL758oJHETRU8AEKx4IAB5CQNYGiw4CxeACNYbGgFdupkDADcU2eGgKkytQC8N23foxsPEJicy9LGztHZxd5BAA3dSg2NRh4AB9VKC8nKAA6UkpaRiY8kV0GOGQrCCF4AF4xYKkmJyA)\r\n\r\n**Expected behavior:**\r\n`a.doThing()` should have type `Promise\u003cBar | Baz>`. `result` should have type `Bar | Baz`.\r\n\r\n**Actual behavior:**\r\n`a.doThing()` has the type `Promise\u003cBar> | Promise\u003cBaz>`. The `.then` call has an \"This expression is not callable\" error on it:\r\n```\r\nThis expression is not callable.\r\n Each member of the union type '(\u003cTResult1 = Bar, TResult2 = never>(onfulfilled?: ((value: Bar) => TResult1 | PromiseLike\u003cTResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike\u003c...>) | null | undefined) => Promise\u003c...>) | (\u003cTResult1 = Baz, TResult2 = never>(onfulfilled?: ((value: Baz) => TResult1 | PromiseLike\u003c...>) |...' has signatures, but none of those signatures are compatible with each other.(2349)\r\n```\r\n\r\nThe same behavior is observable using `Array` and `.map` rather than `Promise` and `.then`: https://www.typescriptlang.org/play/?ts=3.8.0-dev.20200118&ssl=1&ssc=1&pln=16&pc=1#code/CYUwxgNghgTiAEkoGdnwGIHtPwN4Ch4j5hMAVACwEsA7AcwAoBKALngEEYYoBPAHgAu1ZAD58AX3z5QSOImip4AIVjwQADwEgawNFhwFi8AEaw2NAK4BbYyBgBuCdPDQ5SRSoBeazdt0ZsPEJiU09za1sHJ2dZBAA3VSg2FRh4AB9lKE9HKAA6UkpaRiZcqygABwY4ZAsIAXgAXhEgiSZHIA\r\n\r\nThis behavior is what I *think* is appropriate here since `Bar` and `Baz` are both subclasses of `Foo`, and therefore the `doThing` function being called is the same for both types in the union. I can see that outside of this specific situation, calling a function on a union type should return the union of the possible return types. However, I think it would be better if the behavior could be different for subclasses somehow.\r\n\r\nIf not, what sort of type assertion could I use to get around this?\r\n\r\n**Related Issues:** #35953 seems kinda similar, but I can't make much sense of the example given there myself.\r\n","author":{"url":"https://github.com/eritbh","@type":"Person","name":"eritbh"},"datePublished":"2020-01-19T19:28:41Z","url":"https://github.com/microsoft/TypeScript/issues/36307"}
| 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:adc6c7ba-a25e-8899-1152-e96aa48f389f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C454:31B4CC:93CBE8:C48FE4:6B00E12E |
| html-safe-nonce | ec27e27af709f2a9e82fa07a739f0abd9860535cdfdb381f703097da7a879103 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNDU0OjMxQjRDQzo5M0NCRTg6QzQ4RkU0OjZCMDBFMTJFIiwidmlzaXRvcl9pZCI6IjEyMTU1ODg0NTUwOTUwMDEzOTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d7ee22a189a135558242563bd1dba947f5fda0321c29a46c66d6abafa20a3649 |
| hovercard-subject-tag | issue:551971377 |
| 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/36307/issue_layout |
| twitter:image | https://opengraph.githubassets.com/25257d4398680fb80dba1a9d97b935086be96f804da4e40bf31be6d4968871a8/microsoft/TypeScript/issues/36307 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/25257d4398680fb80dba1a9d97b935086be96f804da4e40bf31be6d4968871a8/microsoft/TypeScript/issues/36307 |
| og:image:alt | TypeScript Version: 3.8.0-dev.20200118 Search Terms: union of subclasses with "this" generic Code declare class Foo { doThing(): Promise |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | eritbh |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| 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 | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width