-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
[Suggestion] Multiple catch-blocks #53965
Comments
Duplicate of #30830. And the viability checklist is wrongly checked. |
That's some automated information that may or may not align with the repositories usual workflow. I don't think re-suggesting features when they've been explicitly declined is a good approach. The requested feature is still out of scope for TypeScript. |
Thank you for your comments, @MartinJohns. My proposal differs from #30830 insofar as it requires no type-to-runtime translation of type annotation but relies on identifiers available at runtime. As for design goals, while this is "new syntax sugar for JS", such features do still exist (such as parameter property initialization), and similar exception may then also be made in other cases. |
The only difference I can see is the position of the type. Otherwise it's exactly the same (except the re-throw behaviour).
These features stem from a dark time where the team did not have the Language Design Goals they have nowadays. Rewriting syntax like this (except for downleveling to support older ECMAScript versions) is a big no-go. But I said my bit, I'm out. |
Everything @MartinJohns said is correct
Not really germane either way, but FWIW this is GitHub Refined's UI, but GitHub itself |
Suggestion
π Search Terms
Multiple catch, catch instanceof, typed catch
β Viability Checklist
My suggestion meets these guidelines:
π Motivating Example
Previously, this would have required a much more nested and verbose body, such as:
β Suggestion
This suggestion extends the syntax of the catch statement by allowing one or more identifiers to restrict the type of errors that would be caught:
This would roughly translate to:
To allow for multi-catch blocks, identifiers grouped with
a | b | c
translate to||
'edinstanceof
expressions.If no catch block without annotation is provided, the error is re-thrown:
turns into
No catch blocks may come after the "untyped" catch block, or else an unrecoverable syntax error is raised.
The types of the identifiers provided must satisfy the conditions for being used on the right-hand side of an instanceof expression and not be
any
orunknown
.If one of the provided annotations is of type
any
orunknown
, a semantic error is raised.For annotated catch blocks:
If not provided, the type of the variable of the catch statement is inferred to the type of the identifier used to restrict the clause.
If provided, it must be a supertype of that type or a semantic error is raised.
Type inference of the unannotated catch block (i.e.
catch (err: unknown)
) is not altered, hereby making the proposal fully backwards-compatible.π» Use Cases
The goal of this proposal aligns with other languages' goals that also support multiple catch blocks:
Better code readability:
I would be willing to work on a PR.
The text was updated successfully, but these errors were encountered: