Closed
Description
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
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript codeThis wouldn't change the runtime behavior of existing JavaScript codeThis could be implemented without emitting different JS based on the types of the expressionsThis 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.
Activity
phiresky commentedon Mar 23, 2020
This would be great (together with a lint that forces doing this always).
Right now, catch (e) {} is basically a situation that should be forbidden by
noImplicitAny
, except it can't be because you can't annotate it with any type (except for unknown) since that would be misleading.bradzacher commentedon Mar 27, 2020
I would really love this as a compiler option.
Being able to set a flag that makes all caught exceptions
unknown
instead of manually annotating them would be much better, but I guess it would be harder to migrate to.hediet commentedon Mar 29, 2020
I think I would also prefer a new
strict*
compiler option over new syntax (that would need to be enforced with a lint rule).I think type safety is not the only issue here. Unexperienced TypeScript developers would think that this catch statement might only handle errors of type
SpecialError
!Raynos commentedon May 5, 2020
👍 this would be amazing. To allow a type annotation on
catch
but only if itserr: unknown
MicahZoltu commentedon May 9, 2020
Assuming this is the appropriate place to discuss this, I would like to advocate that the change in 4.0 be either (in order of preference):
catch (error)
results inerror
having a type ofunknown
error
be of typeunknown
catch (error: unknown)
I understand that
error
isany
for legacy reasons (it was beforeunknown
existed) and therefore can't be trivially changed (at least without a major version bump). However, I would like to see a path that allows us to get away from that eventually, either with a breaking change (error
isunknown
) or with a new compiler setting that is flipped on with thestrict
flag.Raynos commentedon May 9, 2020
I am excited typescript 4.0 will address this issue.
We had to add a specific
--ignore-catch
flag to thetype-coverage
command ( https://github.com/plantain-00/type-coverage#ignore-catch ) to handle the fact thatcatch {}
is the only place an implicitany
is mandatory and unavoidable.ExE-Boss commentedon May 31, 2020
It should probably also be possible to do:
In case
unknown
ever becomes the default inferred type (e.g.: #27265).52 remaining items