Title: Generic parameter inference for Promise.then oddity · Issue #11022 · microsoft/TypeScript · GitHub
Open Graph Title: Generic parameter inference for Promise.then oddity · Issue #11022 · microsoft/TypeScript
X Title: Generic parameter inference for Promise.then oddity · Issue #11022 · microsoft/TypeScript
Description: TypeScript Version: 1.8.0 / nightly (2.0.5-dev.20160919) Code interface Thenable
Open Graph Description: TypeScript Version: 1.8.0 / nightly (2.0.5-dev.20160919) Code interface Thenable
X Description: TypeScript Version: 1.8.0 / nightly (2.0.5-dev.20160919) Code interface Thenable<T> { then<U>(then: (value: T) => U | Thenable<U>): Thenable<U>; } declare let x: Thenable...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/11022
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generic parameter inference for Promise.then oddity","articleBody":"**TypeScript Version:** 1.8.0 / nightly (2.0.5-dev.20160919)\n\n**Code**\n\n``` ts\ninterface Thenable\u003cT> {\n then\u003cU>(then: (value: T) => U | Thenable\u003cU>): Thenable\u003cU>;\n}\n\ndeclare let x: Thenable\u003cstring>;\ndeclare function f(): Thenable\u003cstring|null>|null;\ndeclare function g(): Thenable\u003cstring>;\n\nlet y = x.then((_) => {\n if (Math.random() \u003c 0.5) {\n return f();\n } else {\n return g();\n }\n});\n```\n\n**Expected behavior:**\n`U` should be inferred to be `string | null` as that solves the type constraints. It can be verified by explicitly adding `then\u003cstring | null>.(...)` which type checks.\n\n**Actual behavior:** \nTS errors with `Argument of type '(_: string) => Thenable\u003cstring> | null' is not assignable to parameter of type '(value: string) => string | Thenable\u003cstring>'.`\n\n**Oddities:**\nThe following changes make the error disappear, while I expect them to have no relevance to the type of the closure:\n- moving line 5 to line 8 - `declare let x` below `declare function f` and `g`.\n- change of the type of x to `Thenable\u003cnumber>`.\n- add a field of type T to Thenable - `a: T`, that would make `Thenable` strictly covariant.\n- replace with ternary expression - `(_) => (Math.random() \u003c 0.5) ? f() : g()`\n","author":{"url":"https://github.com/rkirov","@type":"Person","name":"rkirov"},"datePublished":"2016-09-20T23:59:13Z","url":"https://github.com/microsoft/TypeScript/issues/11022"}
| 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:8a39d991-617a-3849-b778-2ca99288d2a3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B2B8:37820D:C6B1BA:10A41D9:6B00EC93 |
| html-safe-nonce | d8f182686bb97ac6246e2fe784b5091975a527b068bd6b180a5d5bb3fe5101e5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMkI4OjM3ODIwRDpDNkIxQkE6MTBBNDFEOTo2QjAwRUM5MyIsInZpc2l0b3JfaWQiOiI3NDc1MzUyNjk0NTc2NzA0NjU5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9a00dc62fcedf2db6a30f2706279b0c21cd7d2fa1fe9fce416d781c0673f7d36 |
| hovercard-subject-tag | issue:178211232 |
| 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/11022/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2fd938a71b0b7a0f4c9e0b4cfaca88c90da2dd0664df46c76475e32bae4d84cd/microsoft/TypeScript/issues/11022 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2fd938a71b0b7a0f4c9e0b4cfaca88c90da2dd0664df46c76475e32bae4d84cd/microsoft/TypeScript/issues/11022 |
| og:image:alt | TypeScript Version: 1.8.0 / nightly (2.0.5-dev.20160919) Code interface Thenable |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rkirov |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width