IssueRaiser

Allow type annotation on catch clause variable

**TypeScript Version:** 2.6.1 ```ts const rejected = Promise.reject(new Error()); async function tryCatch() { try { await rejected; } catch (err: Error) { // TS1196: Catch clause variable cannot have a type annotation // Typo, but `err` is `any`, so it results in runtime error console.log(err.mesage.length); } } function promiseCatch() { rejected.catch((err: Error) => { // OK // Compiler error; Yay! console.log(err.mesage.length); }); } ``` This was discussed in #8677 and #10000. It was closed as "fixed" in #9999, but as far as I can tell neither of the issues was actually resolved. In either event, I'd like to make a case for allowing type annotations in catch clauses. Especially with the introduction of downlevel async functions, I'd suggest that disallowing catch clause type annotations leads to less safe code. In the example, the two methods of handling the promise are functionally equivalent, but one allows you to type the error, and the other doesn't. Without writing _extra_ code for the `try`/`catch` version (`if (err instanceof Error) {` or `const e: Error = err` or something), you'll get a runtime error that you wouldn't get with the pure `Promise` version. The primary rationale for not allowing this is that any object can be thrown, so it's not guaranteed to be correct. However, most of the benefit of TypeScript comes from making assertions about your and other people's code that can't be strictly guaranteed (especially when impo

Posted by jaredru on Nov 15, 2017, 1:00 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments

github (Marketing)

Allow type annotation on catch clause variable

jaredruNov 15, 2017, 1:00 AM

**TypeScript Version:** 2.6.1 ```ts const rejected = Promise.reject(new Error()); async function tryCatch() { try { await rejected; } catch (err: Error) { // TS1196: Catch clause variable cannot have a type annotation // Typo, but `err` is `any`, so it results in runtime error console.log(err.mesage.length); } } function promiseCatch() { rejected.catch((err: Error) => { // OK // Compiler error; Yay! console.log(err.mesage.length); }); } ``` This was discussed in #8677 and #10000. It was closed as "fixed" in #9999, but as far as I can tell neither of the issues was actually resolved. In either event, I'd like to make a case for allowing type annotations in catch clauses. Especially with the introduction of downlevel async functions, I'd suggest that disallowing catch clause type annotations leads to less safe code. In the example, the two methods of handling the promise are functionally equivalent, but one allows you to type the error, and the other doesn't. Without writing _extra_ code for the `try`/`catch` version (`if (err instanceof Error) {` or `const e: Error = err` or something), you'll get a runtime error that you wouldn't get with the pure `Promise` version. The primary rationale for not allowing this is that any object can be thrown, so it's not guaranteed to be correct. However, most of the benefit of TypeScript comes from making assertions about your and other people's code that can't be strictly guaranteed (especially when impo

Comments (0)

No comments yet. Start the discussion.

Comment on this issue