-
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
Allow "catch (e as Error)" instead of "catch (e: unknown)" #42596
Comments
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. |
Yes! I think that would be a good trade-off for situations where we can be sure to receive an "Error" object! |
Duplicate of #20024 |
@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 The rationale given above was:
These points apply to That said it does look like @DanielRosenwasser suggested the same idea in #20024 (comment):
|
I think that The practical result of not having a syntax like this is that people just use the default
Adding |
It's worth pointing out that the TypeScript code base builds with Maybe the priority for this problem would be more apparent if the compiler developers had to use their own feature. 😋 |
@MickeyPhoenix This is a really great suggestion. I'm going to update the issue description to include your idea. |
Thanks! I wish I could take credit for it, but actually it's from @jaredru's comment (here) as highlighted by @DanielRosenwasser (here). I just spelled out why I think it's such a great idea. :-) As much as I'd like to, I can't quite support the |
Setting Turns out it requires TypeScript 4.4 however (which was surprising since the problematic validation was introduced in 4.0). As soon as I added the setting to our rig, we started seeing breaks in projects that build using an older compiler. Just something to be aware of. It would be super awesome if |
Suggestion
TypeScript 3.x forbids type annotations in
catch
blocks:But TypeScript 4.x relaxed this rule to accept
any
orunknown
only:⭐ 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:
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.catch
block to perform paranoid runtime tests forinstanceof Error
.instanceof Error
for transpiled code.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:🔍 Search Terms
catch
instanceof
TS1996
1996
✅ Viability Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: