You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Today I had another situation where I would have wanted #13219, but a less complex feature that would have prevented the bug as well would have been if the error in catch clauses had the new unknown type instead of any, forcing me to narrow it down before accessing properties.
Ideally this would be the same for the Promise rejection type.
Use Cases
Making error handling more type safe. Currently any prevents type narrowing.
I'd love to see this as well. I just made a PR to get this for Reacts componentDidCatch (DefinitelyTyped/DefinitelyTyped#30068), because it would have prevented a real world bug in our code and looked for an equivalent issue for the native try/catch.
I believe the default inferred type for catch (err) { } should be unknown, which it is, instead of any which is unsafe and highly discouraged. unknown is safer and provides better semantics; I really don't understand why this is not the default for tsc.
Activity
mhegazy commentedon Aug 3, 2018
This is only possible if #25720, #10715, #25172, and #21732 are implemented. otherwise it is a massive breaking change.
donaldpipowitch commentedon Oct 28, 2018
I'd love to see this as well. I just made a PR to get this for Reacts
componentDidCatch(DefinitelyTyped/DefinitelyTyped#30068), because it would have prevented a real world bug in our code and looked for an equivalent issue for the nativetry/catch.falsandtru commentedon Jan 3, 2020
I want to enable this change by a compiler option. Or want to declare only unknown type like
catch (reason: unknow) { }.gerardolima commentedon May 5, 2021
I believe the default inferred type for
catch (err) { }should beunknown, which it is, instead ofanywhich is unsafe and highly discouraged.unknownis safer and provides better semantics; I really don't understand why this is not the default for tsc.please see: typescript-eslint/typescript-eslint#3348
wbt commentedon Dec 17, 2021
It appears this has been implemented in #41013 and should likely be closed.
https://devblogs.microsoft.com/typescript/announcing-typescript-4-4-beta/#use-unknown-catch-variables
docs: real-world bugs this design would have prevented