Title: empty return statement should be a compiler error when return type is specified · Issue #5916 · microsoft/TypeScript · GitHub
Open Graph Title: empty return statement should be a compiler error when return type is specified · Issue #5916 · microsoft/TypeScript
X Title: empty return statement should be a compiler error when return type is specified · Issue #5916 · microsoft/TypeScript
Description: Currently this function compiles just fine function foo(x): string { return; } I understand that it's happening because undefined is a valid result for a variable of type string, and this sort of related back to #185, but a midway point ...
Open Graph Description: Currently this function compiles just fine function foo(x): string { return; } I understand that it's happening because undefined is a valid result for a variable of type string, and this sort of r...
X Description: Currently this function compiles just fine function foo(x): string { return; } I understand that it's happening because undefined is a valid result for a variable of type string, and this sort ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/5916
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"empty return statement should be a compiler error when return type is specified","articleBody":"Currently this function compiles just fine\n\n``` .ts\nfunction foo(x): string {\n return;\n}\n```\n\nI understand that it's happening because `undefined` is a valid result for a variable of type `string`, and this sort of related back to #185, but a midway point could be to simply require the return statement to not be empty.\n\nSimilarly, this code fails to compile:\n\n``` .ts\nfunction foo(x): string {\n}\n```\n\nBut this one compiles:\n\n``` .ts\nfunction foo(x): string {\n if(false) {\n return;\n }\n}\n```\n\nI think the expected behavior is that every branch should require an explicit return statement.\n\nA side benefit of this is that you can get exhausting matching completeness basically for free:\n\n``` .ts\nfunction foo(x: number | string | boolean): string {\n if(typeof x === 'number') {\n return x.toString();\n } else if (typeof x === 'string') {\n return x;\n }\n}\n```\n\nCurrently the above compiles, but if TypeScript required an explicit return in every branch for non void return functions, there would be a compiler error because there are remaining types in the union. However this would compile:\n\n``` .ts\nfunction foo(x: number | string | boolean): string {\n if(typeof x === 'number') {\n return x.toString();\n } else if (typeof x === 'string') {\n return x;\n } else if (typeof x === 'boolean') {\n return ''+x;\n }\n}\n```\n\nThis is pretty nice! There is one edge case still -- because of the implicit `void` included in `x`, calling `foo(null)` or `foo(undefined)` will return `undefined`. I don't know what to do about this because it relates pretty heavily to #185. I think for now it can be ignored, and whatever resolution that (hopefully) happens there will take care of it.\n","author":{"url":"https://github.com/tejacques","@type":"Person","name":"tejacques"},"datePublished":"2015-12-03T21:23:57Z","url":"https://github.com/microsoft/TypeScript/issues/5916"}
| 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:dade6ee7-b787-d0a6-9107-e5cb602310db |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E986:2E9831:473047:5E0128:6B0198E1 |
| html-safe-nonce | 384029cc00cbe937d5ace2d1020e3e8ce9522bb01229ada5e96b00f50a43749d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOTg2OjJFOTgzMTo0NzMwNDc6NUUwMTI4OjZCMDE5OEUxIiwidmlzaXRvcl9pZCI6IjM2NDM5MzU2NDM5NjY0Nzg1NjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d101642a3d90548c1f43f5cfb8253670cb98f83b4770c29b652e74df6878601b |
| hovercard-subject-tag | issue:120273102 |
| 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/5916/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c633013588c1a2a833fa654e94e9606483b531568ceca6878715e928ebb873f4/microsoft/TypeScript/issues/5916 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c633013588c1a2a833fa654e94e9606483b531568ceca6878715e928ebb873f4/microsoft/TypeScript/issues/5916 |
| og:image:alt | Currently this function compiles just fine function foo(x): string { return; } I understand that it's happening because undefined is a valid result for a variable of type string, and this sort of r... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tejacques |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width