-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
TypeScript fails to narrow union of native Error types #60033
Comments
This is working as intended. |
I may understand the dynamics of how these types are simplified but I can't understand how this is the intended behavior. Is it incorrect to expect the behavior I described or is it just a limitation of the actual types definition? |
It's a limitation of the type system. It's essentially the same as |
The "subtype reduction" dynamic is fine, what I didn't get was the fact that different types of native errors had interchangeable definitions. Thanks! |
You may also be interested in #202, as that handles the difference between a plain object that so happens to have all of the properties of |
π Search Terms
"union native error", "type narrowing union error", "instanceof union error".
π Version & Regression Information
Tried in almost every version between 4.0.5 and 5.6.2.
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.6.2#code/GYVwdgxgLglg9mABFApgZygCigfgFyLgDWYcA7mAJSIDeAUIojMItk2BgIaQpwsDKATzBROADwCiAJylwp1eo0ZSUUEFKRQA3A0QBfOruaso7Ljz6IAKoIAOKabPm1dy1es07GB3VAAWsmTIOj5QdiiIaH5wIAA2ACYAQihCIuKOcgDyUjb2GVKIALyIAEruGrkoADxh9paoGAB8dAD0LYg4AHqIqaKSMnJAA
π» Code
π Actual behavior
The inferred return type of the
test
function isSyntaxError
.π Expected behavior
The inferred return type of the
test
function should beSyntaxError | TypeError
.Additional information about the issue
The issue seems to occur with all types of native JavaScript errors, where inferred unions are resolved with just on error (the first defined). Custom errors (classes that extends
Error
) defined in userland do not appear to suffer from this problem.The text was updated successfully, but these errors were encountered: