Title: Methods that always throw aren't always recognized as a return from a function · Issue #39839 · microsoft/TypeScript · GitHub
Open Graph Title: Methods that always throw aren't always recognized as a return from a function · Issue #39839 · microsoft/TypeScript
X Title: Methods that always throw aren't always recognized as a return from a function · Issue #39839 · microsoft/TypeScript
Description: TypeScript Version: 3.9.2 Search Terms: throw, return Code class Logger { throwError() : never { throw Error(); } } class Foo { logger: Logger; constructor() { this.logger = new Logger(); } // does not compile fail(x : number) : number {...
Open Graph Description: TypeScript Version: 3.9.2 Search Terms: throw, return Code class Logger { throwError() : never { throw Error(); } } class Foo { logger: Logger; constructor() { this.logger = new Logger(); } // does...
X Description: TypeScript Version: 3.9.2 Search Terms: throw, return Code class Logger { throwError() : never { throw Error(); } } class Foo { logger: Logger; constructor() { this.logger = new Logger(); } // does...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/39839
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Methods that always throw aren't always recognized as a return from a function","articleBody":"\u003c!-- 🚨 STOP 🚨 STOP 🚨 STOP 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!--\r\nPlease try to reproduce the issue with the latest published version. It may have already been fixed.\r\nFor npm: `typescript@next`\r\nThis is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly\r\n-->\r\n**TypeScript Version:** 3.9.2\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\nthrow, return\r\n**Code**\r\n\r\n```ts\r\nclass Logger {\r\n throwError() : never {\r\n throw Error();\r\n }\r\n}\r\n\r\nclass Foo {\r\n logger: Logger;\r\n\r\n constructor() {\r\n this.logger = new Logger();\r\n }\r\n\r\n // does not compile\r\n fail(x : number) : number {\r\n if (x \u003c 0) {\r\n return x;\r\n } else {\r\n const logger = new Logger();\r\n logger.throwError();\r\n }\r\n }\r\n\r\n // does compile\r\n success1(x : number) : number {\r\n if (x \u003c 0) {\r\n return x;\r\n } else {\r\n this.logger.throwError();\r\n }\r\n }\r\n\r\n // does compile\r\n success2(x: number, logger: Logger) : number {\r\n if (x \u003c 0) {\r\n return x;\r\n } else {\r\n logger.throwError();\r\n }\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\nIn all of these cases, the code should compile because `logger.throwError()` will always throw an error, therefore returning from the current function. \r\n\r\n**Actual behavior:**\r\nHowever, for the `fail` method, you get the compiler error\r\n> Function lacks ending return statement and return type does not include 'undefined'.(2366)\r\n\r\nI'm not sure why the difference between having the logger as a field/parameter vs. a local variable causes some cases to compile but some not.\r\n\r\n**Playground Link:** \u003c!-- A link to a TypeScript Playground \"Share\" link which demonstrates this behavior -->\r\nhttps://www.typescriptlang.org/play/#code/MYGwhgzhAEAyD2BzRBTATtA3gWAFDQOgBcALNeAdwFE1y0AKASmgC5oA7FAN3Sz0IHEylaDTpMA3P0IBfPHNx5QkGADF48PvkIgkqNGwTJ0UxdoLB47CETQBXYEXgNmOc4NIBLCADpdxjABeDhQKOD10SWkCBWjoAHp46AATeBQYdngiaEsAWwAHTxAUOIAzMCL6AA9WDjtcgCN0ZjZ2eqaMN0FCT1LoaugAHmgABlc47oI0FCI7NHZoKtNJ6BloFBAIFC0VgUtrbP99aGDOMKN9KPcVo-QfUnJqWmcrlYUBWPdElLSYPMLinEIA5gOkIABGAatdrNWptRq8LqTXr9GrDMY7XZTGZzBZLCaydabbZIrFeXy3ND3YRPcSMZaTd6yPBxb6pdI5eAFIoldzA4CgqAAJmq0IRaAANNBKYYImgWnVxZjuiiBujxtdJtNZvNFgzumsNltlbtKdTHmIXvSCTE4goZEA\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\n","author":{"url":"https://github.com/yagoldin","@type":"Person","name":"yagoldin"},"datePublished":"2020-07-30T21:38:05Z","url":"https://github.com/microsoft/TypeScript/issues/39839"}
| 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:e08f0361-5e6f-1bfe-ea7b-15cd8932f702 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AA80:3DD488:9ECBCC:CACCB6:6B00F25B |
| html-safe-nonce | 277ef3d2d236ffe65d27c0c575ce3b6117dba09a9da70e1353d49feeca22aa34 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTgwOjNERDQ4ODo5RUNCQ0M6Q0FDQ0I2OjZCMDBGMjVCIiwidmlzaXRvcl9pZCI6IjU5MjcyODIwMjI1MDU1NzUwMDMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6f87c454c5572ccbfa6ac2e282bf1f237030a6e08205fbd6f4c91ef0a1df2859 |
| hovercard-subject-tag | issue:669216777 |
| 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/39839/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c044fb545eb7632e9a717481d04d06b07c5d2c483d71c6f68260e236e6c05cbe/microsoft/TypeScript/issues/39839 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c044fb545eb7632e9a717481d04d06b07c5d2c483d71c6f68260e236e6c05cbe/microsoft/TypeScript/issues/39839 |
| og:image:alt | TypeScript Version: 3.9.2 Search Terms: throw, return Code class Logger { throwError() : never { throw Error(); } } class Foo { logger: Logger; constructor() { this.logger = new Logger(); } // does... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yagoldin |
| 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