Search Terms
catch clause unknown exception
Suggestion
Now any kind of type annotation on catch clauses is not allowed.
In my understanding, it's because that it's unsafe to annotate like catch (err: SpecialError) since any value will be captured actually.
However, due to the type of err is any, it's also not type safe.
So I suggest to allow annotating unknown , as most safe typing.
(And annotation with any other type won't be allowed as is)
Examples
try {
throw 42;
} catch (err) {
console.error(err.specialFunction());
}
can be written safer as
try {
throw 42;
} catch (err: unknown) {
if (err instanceof SpecialError) {
console.error(err.specialFunction());
} else {
...
}
}
Related Issue
#20024
Checklist
My suggestion meets these guidelines: