Title: Allow `: { }` / `: any` type annotations on catch variables · Issue #10000 · microsoft/TypeScript · GitHub
Open Graph Title: Allow `: { }` / `: any` type annotations on catch variables · Issue #10000 · microsoft/TypeScript
X Title: Allow `: { }` / `: any` type annotations on catch variables · Issue #10000 · microsoft/TypeScript
Description: It was pointed out in #9999 that catch variables never get narrowed because they're of type any, and you can't change their type because they're not allowed to have type annotations. This leads to pitfalls like try { // do something } ca...
Open Graph Description: It was pointed out in #9999 that catch variables never get narrowed because they're of type any, and you can't change their type because they're not allowed to have type annotations. This leads to ...
X Description: It was pointed out in #9999 that catch variables never get narrowed because they're of type any, and you can't change their type because they're not allowed to have type annotations. Th...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/10000
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow `: { }` / `: any` type annotations on catch variables","articleBody":"It was pointed out in #9999 that `catch` variables never get narrowed because they're of type `any`, and you can't change their type because they're not allowed to have type annotations.\n\nThis leads to pitfalls like\n\n``` ts\ntry {\n // do something\n} catch(e) {\n if(e instanceof MyError) {\n console.log(e.filname); // oops.\n }\n}\n```\n\nProposal: allow type annotations on catch variables as long as the annotated type is exactly `any` or `{ }` (we can't reasonably make this an implicit any according to `--noImplicitAny` so that behavior would be unchanged). But now \"safer\" developers can write\n\n``` ts\ntry {\n // do something\n} catch(e: { }) {\n if(e instanceof MyError) {\n console.log(e.filname); // error, property not found\n }\n console.log(e.filename); // error, forgot to narrow with a guard\n}\n```\n\nAlternatively (or in addition to), we could just let `catch` variables get narrowed despite being `any`, but this would be a fairly odd carve-out.\n","author":{"url":"https://github.com/RyanCavanaugh","@type":"Person","name":"RyanCavanaugh"},"datePublished":"2016-07-28T05:01:18.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/10000/TypeScript/issues/10000"}
| 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:8a31a34a-f820-ca88-3856-e4860ec1edc9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ACB4:201877:2B4646B:3BDF97B:6A63324D |
| html-safe-nonce | 426762c2490388456c036ceb28304758a3b421d3a74752c09ca19efa0e5c0174 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQ0I0OjIwMTg3NzoyQjQ2NDZCOjNCREY5N0I6NkE2MzMyNEQiLCJ2aXNpdG9yX2lkIjoiMjkxMTA4MzM3NzA3OTgyNDk3MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 109969c01437f37f8d54f56b7adbdf4a7424df7c26e2ce2f75245a0b7be293fc |
| hovercard-subject-tag | issue:168014318 |
| 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/10000/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3659443d8c65825b0728c15f98925e76c6e6c558ab9a5eb7c271ede4c5f67372/microsoft/TypeScript/issues/10000 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3659443d8c65825b0728c15f98925e76c6e6c558ab9a5eb7c271ede4c5f67372/microsoft/TypeScript/issues/10000 |
| og:image:alt | It was pointed out in #9999 that catch variables never get narrowed because they're of type any, and you can't change their type because they're not allowed to have type annotations. This leads to ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | RyanCavanaugh |
| hostname | github.com |
| expected-hostname | github.com |
| None | df6e1c2adcceb061ec8c4106a1b34f38280f08e13f2b195c661c202834cbc3b1 |
| 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 | 822a0ff2a479845adcdd40a1a7b1734a56e05818 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width