Title: Can't narrow error type in catch clause · Issue #8677 · microsoft/TypeScript · GitHub
Open Graph Title: Can't narrow error type in catch clause · Issue #8677 · microsoft/TypeScript
X Title: Can't narrow error type in catch clause · Issue #8677 · microsoft/TypeScript
Description: try { ... } catch (ex) { // Adding an annotation here gives ERROR TS1196: Catch clause variable cannot have a type annotation if (ex instanceof FooError) { ex; // ex is of type any ex.message; // OK ex.iDontExist; // typechecks OK, but d...
Open Graph Description: try { ... } catch (ex) { // Adding an annotation here gives ERROR TS1196: Catch clause variable cannot have a type annotation if (ex instanceof FooError) { ex; // ex is of type any ex.message; // O...
X Description: try { ... } catch (ex) { // Adding an annotation here gives ERROR TS1196: Catch clause variable cannot have a type annotation if (ex instanceof FooError) { ex; // ex is of type any ex.message; // O...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/8677
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Can't narrow error type in catch clause","articleBody":"``` ts\n\ntry {\n ...\n}\ncatch (ex) { // Adding an annotation here gives ERROR TS1196: Catch clause variable cannot have a type annotation\n if (ex instanceof FooError) {\n ex; // ex is of type any\n ex.message; // OK\n ex.iDontExist; // typechecks OK, but doesn't exist\n }\n else if (isBarError(ex)) {\n ex; // ex is of type any\n ex.foo(); // typechecks OK, but runtime error\n }\n else {\n ...\n }\n}\n```\n\nI guess this is expected behaviour not a bug. But I was not previously aware that the catch clasue variable cannot be annotated. Due to #1426, this means we can't narrow `ex` inside the catch clause, so all the cases dealing with specific error types get no type checking (since `ex` remains as `any`).\n","author":{"url":"https://github.com/yortus","@type":"Person","name":"yortus"},"datePublished":"2016-05-19T00:43:12.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":24},"url":"https://github.com/8677/TypeScript/issues/8677"}
| 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:991f23ad-6c87-ac51-e754-7ad726c53031 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8482:1AC9BC:13A5C48:1A7AC52:6A62CE34 |
| html-safe-nonce | 94939959cd48254d993617ec1760c464d41324e6f1e28151e3644a1ecafc505a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NDgyOjFBQzlCQzoxM0E1QzQ4OjFBN0FDNTI6NkE2MkNFMzQiLCJ2aXNpdG9yX2lkIjoiMjkwNDM0NTE3NDY4Nzc5Njc4OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 93487489ce81cc590e25aceba673c053917cd7fa6d00c563988e4320b298c909 |
| hovercard-subject-tag | issue:155632079 |
| 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/8677/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7db4f68f79aa2415b5dd890b0f08bec69b0ca7f934169cbf0034c09b3528b9c3/microsoft/TypeScript/issues/8677 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7db4f68f79aa2415b5dd890b0f08bec69b0ca7f934169cbf0034c09b3528b9c3/microsoft/TypeScript/issues/8677 |
| og:image:alt | try { ... } catch (ex) { // Adding an annotation here gives ERROR TS1196: Catch clause variable cannot have a type annotation if (ex instanceof FooError) { ex; // ex is of type any ex.message; // O... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yortus |
| hostname | github.com |
| expected-hostname | github.com |
| None | df33b1b61ee7b9a0af988199bfc3503c9c1acafb1f1d40e1f140ea7c84f890dd |
| 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 | d41cd1bdb290013455c0ac430fa755621733f5eb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width