Title: Handle union type of void and Promise
Open Graph Title: Handle union type of void and Promise
X Title: Handle union type of void and Promise
Description: Suggestion 🔍 Search Terms void, async, second-order functions ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior...
Open Graph Description: Suggestion 🔍 Search Terms void, async, second-order functions ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code T...
X Description: Suggestion 🔍 Search Terms void, async, second-order functions ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript co...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/43921
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Handle union type of void and Promise\u003cvoid> in return types","articleBody":"# Suggestion\r\n\r\n## 🔍 Search Terms\r\n\r\nvoid, async, second-order functions\r\n\r\n## ✅ Viability Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [X] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [X] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [X] This could be implemented without emitting different JS based on the types of the expressions\r\n* [X] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [X] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n## ⭐ Suggestion (and motivating example)\r\n\r\n(I apologize if there is already discussion around this; I haven't been able to find it.)\r\n\r\nIt is sometimes convenient for a second-order function to receive a function that may or may not be asynchronous:\r\n\r\n```\r\nfunction mySecondOrderFn(firstOrderFn: () => void | Promise\u003cvoid>) {\r\n return async () => {\r\n console.log(\"Calling firstOrderFn()\")\r\n const output = await firstOrderFn()\r\n console.log(\"Finished calling firstOrderFn()\")\r\n } \r\n}\r\n\r\n// @ts-expect-error I'd like to be able to do this:\r\nmySecondOrderFn(() => 2)\r\n\r\n// @ts-expect-error Or this, for that matter:\r\nmySecondOrderFn(async () => 2)\r\n```\r\n\r\nUnfortunately `() => void | Promise\u003cvoid>` seems to cancel TS's normal type inference around `() => void`:\r\n```\r\nconst x: () => void = () => 2\r\n// @ts-expect-error Type 'number' is not assignable to type 'void | Promise\u003cvoid>\r\nconst y: () => void | Promise\u003cvoid> = () => 2\r\n```\r\n\r\nIn addition (this is probably the root cause): `() => Promise\u003cvoid>` by itself is pickier than I would expect since only `() => Promise\u003cundefined>` and `() => Promise\u003cany>` are assignable to it. I understand that `void` is not an inferrable type but this case does not require `void` to be inferred, does it?\r\n\r\nI suspect this problem exists because of how `void` interacts with parameterized types. Maybe there isn't a general solution to that, but at least I think `() => Promise\u003c2> extends () => Promise\u003cvoid>` would be desirable to support as a special case, given the ubiquity of the `async/await` pattern.\r\n\r\n## 💻 Use Cases\r\n\r\nIn my case, I'm writing an implementation of forEach. It is a context that applies backpressure so I want the first-order function to be able to return a Promise, but otherwise I don't care about the output.\r\n\r\n`() => any` does suffice here; `() => any | Promise\u003cany>` is marginally more expressive, although from a type perspective it is exactly the same. In either case I am exposed to the pitfalls of the `any` type, in particular that is easy to assign such output to a value by mistake (precisely what `void` exists to prevent you from doing).\r\n","author":{"url":"https://github.com/MattiasMartens","@type":"Person","name":"MattiasMartens"},"datePublished":"2021-05-02T19:52:28Z","url":"https://github.com/microsoft/TypeScript/issues/43921"}
| 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:8259311a-cd66-5258-6dfa-e9096259c672 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 854E:1DCE83:58C772:7889CB:6B043FD8 |
| html-safe-nonce | 63d9f9e4305850a2518e3081f9f8a861421e8fc0ecea2a8d182e9aae17e88f17 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NTRFOjFEQ0U4Mzo1OEM3NzI6Nzg4OUNCOjZCMDQzRkQ4IiwidmlzaXRvcl9pZCI6IjkyNTQwNzI5MTkxMjc2NTQwMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6808ece3cecb02ebda96522883d17f8af6f361c27a1c014731dfd50e0cc3aa53 |
| hovercard-subject-tag | issue:874028849 |
| 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/43921/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ab8f0fc7ab1d5a940f695d34dc5466eb59580afac877c3fe8fe76aa96a2b6fe1/microsoft/TypeScript/issues/43921 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ab8f0fc7ab1d5a940f695d34dc5466eb59580afac877c3fe8fe76aa96a2b6fe1/microsoft/TypeScript/issues/43921 |
| og:image:alt | Suggestion 🔍 Search Terms void, async, second-order functions ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code T... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | MattiasMartens |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9185ccfefd80c1ff2d9ddcdb0cd2becf46b5a1efbbf30741a895e4bb8de25e64 |
| 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 | 06dc448a76fe4751ac1db26524aefa3aa3e889a8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width