question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow "catch (e: Error)" instead of "catch (e: unknown)"

See original GitHub issue

Suggestion

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:

  1. In a professional code base, thrown objects always implement the Error interface. 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.
  2. It’s wasteful for every single catch block to perform paranoid runtime tests for instanceof Error.
  3. The TypeScript compiler doesn’t even support instanceof Error for transpiled code.
  4. Relaxing this rule won’t cause any trouble; if some people really prefer unknown they 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:open
  • Created 3 years ago
  • Reactions:41
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
octogonzcommented, Oct 28, 2021

@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:

  1. In a professional code base, thrown objects always implement the Error interface. 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.
  2. It’s wasteful for every single catch block to perform paranoid runtime tests for instanceof Error.
  3. The TypeScript compiler doesn’t even support instanceof Error for transpiled code.
  4. Relaxing this rule won’t cause any trouble; if some people really prefer unknown they can enable it via a lint rule like no-implicit-any-catch without any involvement from the compiler.

These points apply to Error because 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:

I do like the idea of catch (err as Error) because, damn it, if you were going to cast it anyway, maybe we should make your life a little easier while keeping it explicit.

5reactions
lightrowcommented, Sep 15, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found