Allow "catch (e: Error)" instead of "catch (e: unknown)"
See original GitHub issueSuggestion
TypeScript 3.x forbids type annotations in catch blocks:
try {
// ...
} catch (e: Error) { // <-- TS1996 "Catch clause variable cannot have a type annotation"
// ...
}
But TypeScript 4.x relaxed this rule to accept any or unknown only:
try {
// ...
} catch (e: Error) { // <-- TS1996 "Catch clause variable type annotation must be 'any' or 'unknown' if specified"
// ...
}
⭐ Suggestion: Could we relax the rule to allow Error as well?
Such a declaration would not accurately describe pathological JavaScript code.
But here’s why it makes sense anyway:
- In a professional code base, thrown objects always implement the
Errorinterface. We have lint rules that enforce this. And external packages generally follow this rule as well, at least the kind we’d use for professional work. - It’s wasteful for every single
catchblock to perform paranoid runtime tests forinstanceof Error. - The TypeScript compiler doesn’t even support
instanceof Errorfor transpiled code. - Relaxing this rule won’t cause any trouble; if some people really prefer
unknownthey can enable it via a lint rule like no-implicit-any-catch without any involvement from the compiler.
Alternate syntax
In the thread below, @MickeyPhoenix suggested to use as instead of : to clarify that technically this is a type cast, while still keeping the syntax concise and intuitive:
try {
// ...
} catch (e as Error) {
// ...
}
🔍 Search Terms
catch instanceof TS1996 1996
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:41
- Comments:9 (1 by maintainers)
Top Results From Across the Web
How can I safely access caught Error properties in TypeScript?
I am using TypeScript, and have a try / catch block. The caught error is typed ( Error ). I would like to...
Read more >Playground Example - Unknown in Catch - TypeScript
Unknown in Catch. Because JavaScript allows throwing any value, TypeScript does not support declaring the type of an error ...
Read more >Get a catch block error message with TypeScript - Kent C. Dodds
TypeScript forces you to acknowledge you can't know what was thrown making getting the error message a pain. Here's how you can manage...
Read more >Error handling, "try...catch" - The Modern JavaScript Tutorial
But there's a syntax construct try...catch that allows us to “catch” errors so the script can, instead of dying, do something more ...
Read more >Control flow and error handling - JavaScript - MDN Web Docs
A switch statement allows a program to evaluate an expression and ... function f() { try { console.log(0); throw 'bogus'; } catch (e) ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@ExE-Boss There is a slight difference between the two issues: #20024 is asking for arbitrary type annotations for catch blocks, whereas #42596 is /only/ asking for
catch (e: Error)only.The rationale given above was:
Errorinterface. We have lint rules that enforce this. And external packages generally follow this rule as well, at least the kind we’d use for professional work.catchblock to perform paranoid runtime tests forinstanceof Error.instanceof Errorfor transpiled code.unknownthey can enable it via a lint rule like no-implicit-any-catch without any involvement from the compiler.These points apply to
Errorbecause that interface describes the bare minimum properties that any sane error object should have. Whereas these points maybe would NOT be convincing for custom error interfaces.That said it does look like @DanielRosenwasser suggested the same idea in https://github.com/microsoft/TypeScript/issues/20024#issuecomment-344511199:
YES, PLEASE. And ideally also a rule to bring back the TypeScript 3.x behaviour with Error being default type for catch block errors, so we don’t have to specify Error type in every try-catch block
right now I’m forced to annotate all caught errors as “any” which ruins intellisense.