-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Improve catch
with automatic instanceof
checks
#46690
Comments
This is out of scope of TypeScript, and a duplicate of #30830. |
Is there a reason it's out of scope, particularly in light of the |
I think I see in the issue I missed: Typescript does not want to require type-related changes. Although the compiler should be able to generate the checks to avoid dynamic runtime changes, it definitely depends on the the types in the Even so, I'd wholly recommend this be exempted from that. The |
That change doesn't result in different emitted JavaScript. |
It really isn't. First, it's optional, it's a flag. Secondly, it actually improves type-safety by getting rid of the But again, it's optional. You're free to revert back to the old behaviour.
I also don't see what real benefit your suggestion brings, given that you can just simply write the if check yourself and it's just as readable. It's just additional syntax that may result in incompatibilities with future ECMAScript changes. |
I agree with this at face value, but in practice it's going to cause developers to accidentally, silently ignore unexpected errors because of ECMAScript's past mistakes. The example for how to work with it exemplifies this problem: check for the type you expect, but do nothing otherwise. I would rather have the runtime failure in the code than that from the developers making the mistake they're inevitably making from copying the example. That's what I mean by being worse; the type safety is good, but the implicitly new I can fix this in my codebase by disabling the setting, but I can't fix it downstream (or up for that matter).
One time, of course. But what if you're catching and handling errors in 10s, 100s, or 1000s of places in any reasonably complex codebase? The vast majority of codebases would likely be
With the intent that anything else was thrown up the chain untouched, ideally leading to a crash. That definitely cuts down on the boilerplate and code complexity imposed by |
You might want to look into railway-oriented programming, or other more functional approaches to error handling, if your code base is littered with catch-blocks. Having catch-blocks is the exception in code bases I work with. |
Indeed, this is starting to sound suspiciously like a request for checked exceptions, just without the compile-time enforcement. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Suggestion
The recent change that, by default, makes
catch
variables asunknown
(#41016) was an unexpected breaking change.While writing some code to effectively workaround the change, I found myself writing a lot of duplicative, generic code similar to the example. In addition to
err: unknown
being the default behavior, I think that Typescript could add some syntax sugar to simplify code blocks for types that supportinstanceof
checks similar to what Java and C# do:There are obviously legacy codebases that throw strings and other silly "error" types, but the vast majority could be pushed into using structured
Error
types with what amounts to syntax sugar. The emitted JS code could bewith the
err
being rethrown automatically if there is no matchingcatch
clause (e.g., the developer never specifieserr: unknown
).Bonus points if there was an
is
functional way to do it for non-instanceof
types (for codebases that use canonically typedError
objects and, ideally, a built-in version of that that automatically matches toerr: Error
). C# effectively supports this notion as an "exception filter".🔍 Search Terms
catch, unknown, instanceof, Error
Relates to #20024, #42596
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Add Typescript syntax sugar to consistently simplify unknown error type handling.
📃 Motivating Example
No more repetitive code checks for consistently typed codebases for errors! Less errors in your errors!
💻 Use Cases
Stop requiring boilerplate code for every error that will regularly lead to silently ignored errors (including in the example for
err:unknown
itself).The text was updated successfully, but these errors were encountered: