Title: Surprising `Not all code paths return a value` · Issue #18319 · microsoft/TypeScript · GitHub
Open Graph Title: Surprising `Not all code paths return a value` · Issue #18319 · microsoft/TypeScript
X Title: Surprising `Not all code paths return a value` · Issue #18319 · microsoft/TypeScript
Description: TypeScript Version: 2.5.2 Code type AsyncFunction
Open Graph Description: TypeScript Version: 2.5.2 Code type AsyncFunction
X Description: TypeScript Version: 2.5.2 Code type AsyncFunction<T> = () => Promise<T>; const f: AsyncFunction<number | void> = async () => { if (Math.random()) { return 1; } }; Expected b...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/18319
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Surprising `Not all code paths return a value`","articleBody":"**TypeScript Version:** 2.5.2\r\n\r\n**Code**\r\n\r\n```ts\r\ntype AsyncFunction\u003cT> = () => Promise\u003cT>;\r\n\r\nconst f: AsyncFunction\u003cnumber | void> = async () => {\r\n if (Math.random()) {\r\n return 1;\r\n }\r\n};\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nNo tsc errors.\r\n\r\n**Actual behavior:**\r\n\r\n```ts\r\nconst f: AsyncFunction\u003cnumber | void> = async () => {\r\n// ~~~~~~~~~~~~~ [ts] Not all code paths return a value.\r\n\r\n if (Math.random()) {\r\n return 1;\r\n }\r\n};\r\n```\r\n\r\nNotably, when using return-type annotation, this error does not occur:\r\n\r\n```ts\r\nasync function a(): Promise\u003cnumber | void> {\r\n if (Math.random()) {\r\n return 1;\r\n }\r\n}\r\n\r\nconst b = async (): Promise\u003cnumber | void> => {\r\n if (Math.random()) {\r\n return 1;\r\n }\r\n};\r\n```\r\n\r\nIn our real-world scenario, it would not be sufficient to look for this syntactic pattern. Rather, the fact that the async function is assigned to a union containing void should be the trigger that silences this error.","author":{"url":"https://github.com/wycats","@type":"Person","name":"wycats"},"datePublished":"2017-09-07T19:26:21Z","url":"https://github.com/microsoft/TypeScript/issues/18319"}
| 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:71de7cb1-32c3-abe8-a493-314609193045 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BC96:4E3C:C565EE:106CD38:6B00F54D |
| html-safe-nonce | 216641fdc19079fe47211c86c9572afe098f989f92059de0075feb6918ef0721 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQzk2OjRFM0M6QzU2NUVFOjEwNkNEMzg6NkIwMEY1NEQiLCJ2aXNpdG9yX2lkIjoiMzQyMDY0MzEyMDMzNTg3NzQ1MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 006ce6702d3368e14fc50633816a2ada4de51320c76a4badc18dd526aba713d8 |
| hovercard-subject-tag | issue:256044248 |
| 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/18319/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f3304352c338195008ca9acb55f59092ac72e4224ba89197439657157538bf53/microsoft/TypeScript/issues/18319 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f3304352c338195008ca9acb55f59092ac72e4224ba89197439657157538bf53/microsoft/TypeScript/issues/18319 |
| og:image:alt | TypeScript Version: 2.5.2 Code type AsyncFunction |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | wycats |
| 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