try {
// ...
}
catch (e: unknown) {
// error!
// Property 'toUpperCase' does not exist on type 'unknown'.
console.log(e.toUpperCase());
if (typeof e === "string") {
// works!
// We've narrowed 'e' down to the type 'string'.
console.log(e.toUpperCase());
}
}
It would be good to consider these typed/safe (even when --ignore-catch is false).
In TypeScript 4.0 we can finally treat caught exceptions as
unknown
instead ofany
.From https://devblogs.microsoft.com/typescript/announcing-typescript-4-0-beta/#unknown-on-catch:
It would be good to consider these typed/safe (even when
--ignore-catch
is false).See also