Skip to content

Make the type of caught errors unknown instead of any #35719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
devlato opened this issue Dec 17, 2019 · 3 comments
Closed
5 tasks done

Make the type of caught errors unknown instead of any #35719

devlato opened this issue Dec 17, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@devlato
Copy link

devlato commented Dec 17, 2019

Search Terms

catch
unknown
any
catch statement
catch type

Suggestion

Hey, I've noticed that the type of a caught error is any. Should it be unknown instead?

Use Cases

I think the any type makes no real sense in terms of helping process the errors. The unknown has been designed in order to help devs ensure that they narrow down the types properly when the real type is unclear. I believe this type would fit perfectly to the catch statement.

Examples

try {
  somethingThatFailsAndProbablyThrowsAnError();
} catch (e) {
  // e is of unknown type instead of any type, to make devs narrow down the type
  // when they want to do something with the error
  // We have to narrow down the type, before using the value
  if (e instanceof Error) {
    // ... okay
  }
}

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@MartinJohns
Copy link
Contributor

This wouldn't be a breaking change in existing TypeScript/JavaScript code

It would be a heavy breaking change, and that's why it wasn't changed.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Dec 17, 2019
@devlato
Copy link
Author

devlato commented Dec 18, 2019

@MartinJohns Could this be introduced behind a configuration flag then? Ie. --noCatchAny 🙂

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Dec 20, 2019
@RyanCavanaugh
Copy link
Member

Duplicate #26174

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants