Title: Function return value constraints · Issue #228 · microsoft/TypeScript · GitHub
Open Graph Title: Function return value constraints · Issue #228 · microsoft/TypeScript
X Title: Function return value constraints · Issue #228 · microsoft/TypeScript
Description: TypeScript compiler allows to define a function marked explicitly with a return type, with code branches which lead to an inconclusive return value! I know It is valid in JavaScript function not to return a value on all code path, howeve...
Open Graph Description: TypeScript compiler allows to define a function marked explicitly with a return type, with code branches which lead to an inconclusive return value! I know It is valid in JavaScript function not to...
X Description: TypeScript compiler allows to define a function marked explicitly with a return type, with code branches which lead to an inconclusive return value! I know It is valid in JavaScript function not to...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/228
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Function return value constraints","articleBody":"TypeScript compiler allows to define a function marked **explicitly** with a return type, with code branches which lead to an inconclusive return value!\n\nI know It is valid in JavaScript function not to return a value on all code path, however in TypeScript being a javascript **typed** superset, I can imagine having a poor written function [without return value on all code path] could potentially break other codes and I'm talking null-references, type mismatch, `NaN`, ... \nI believe when using TypeScript code in an explicitly typed expression, no-one needs all the ugly plumbings, input or type validation, `undefined` checking, ... One needs to be sure to write a code that couldn't break the way a dynamic language like javascript could!\n\nThe following is a valid JavaScript code and it **could** compile within TypeScript:\n\n``` javascript\nfunction SomeMethod() { }\n```\n\nWhen you explicitly mention that the method has a return value, the compiler throw an error:\n\n``` javascript\nfunction SomeMethod(): number { }\n```\n\nHowever it is not the case when the function does not return in all code path, and I don't understand if this is by design why bother preventing above code from compiling in the first place!\n\nThe followings are all **wrong** in a typed context yet the compiler ignores them as a valid code:\n\n``` javascript\nfunction SomeMethod(): number {\n return;\n}\n\nfunction SomeMethod(): number {\n return null;\n}\n\nfunction SomeMethod(): number {\n if (false)\n return 1;\n}\n\nfunction SomeMethod(): number {\n try {\n return 0;\n }\n catch (ex) {\n }\n}\n```\n","author":{"url":"https://github.com/knazeri","@type":"Person","name":"knazeri"},"datePublished":"2014-07-24T08:09:41Z","url":"https://github.com/microsoft/TypeScript/issues/228"}
| 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:9a524611-a242-448e-bf18-6720cc54a7c2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 921E:3BC53:32F8DB:4494B1:6B0089A4 |
| html-safe-nonce | 91300de812e8656c493f08a7648c5b99c1beaca56963cf4374dd6be126ceb049 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MjFFOjNCQzUzOjMyRjhEQjo0NDk0QjE6NkIwMDg5QTQiLCJ2aXNpdG9yX2lkIjoiMzgxMTc2NTg3NjIxOTIxODM0MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 28eb71c7c47ad397c1496097154a941b12e068dac6077e9800058aed3b4c4fab |
| hovercard-subject-tag | issue:38606127 |
| 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/228/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cef6cfb56bbefc101c6a6d768fc609f71b9c1ef927a3d98398e5b523d287d6d2/microsoft/TypeScript/issues/228 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cef6cfb56bbefc101c6a6d768fc609f71b9c1ef927a3d98398e5b523d287d6d2/microsoft/TypeScript/issues/228 |
| og:image:alt | TypeScript compiler allows to define a function marked explicitly with a return type, with code branches which lead to an inconclusive return value! I know It is valid in JavaScript function not to... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | knazeri |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4d3d8e50f02c1d7bf86d5acfa1d3bfe9366abd6528a6b429eb07a168429d2b61 |
| 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 | db758f8e02b8257935fb03edf81a3e76eb3abd11 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width