-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
TS 5.5 type guards overly strict in some cases #58959
Comments
Interestingly, someone on my team discovered that adding a random getter to
With that in place, the error goes away π€ It has to be a property/method other than |
That's because the types are structurally the same without an additional property. This really didn't change, it was the same behavior in earlier TypeScript versions. Just in earlier versions your This works as intended. See also: https://github.com/microsoft/TypeScript/wiki/FAQ#what-is-structural-typing |
Ah! I see. The fact that it's a different class doesn't matter. Because the types are structurally the same, the types may as well be the same, in the eyes of TypeScript. Makes sense! Closing |
π Search Terms
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?#code/MYGwhgzhAEDKAOBTYBLMICiAnLB7L0iAHgC6IB2AJjNngQN4C+AUMwGYCu5wJKu50FBATI0mHPgAUiCVgBc0WvgCU0es2jQsiEhywCZdQeQgkw3RLjZwkqdEqwBuZi3ZcefAWVOTV6zcD8poSy0AC80OSIAO6Ksr7OmijWkkIiduJ00rLKfhqaWjp65InQLAFBuCCIAHQguADm2XQ1ALaIUGANiMrOLEA
π» Code
π Actual behavior
Error: "Property 'message' does not exist on type 'never'."
Even if
error
isn'tSpecialError
, it's still of typeError
, which means it has amessage
property.π Expected behavior
This should compile. The error not being a SpecialError does not mean the error isn't still of type Error, which has a message property.
Additional information about the issue
In TS 5.4, this was allowed, but it's broken as of 5.5. My initial thought was that this had something to do with automatic type predicates, but adding an explicit type guard to the function doesn't help.
The text was updated successfully, but these errors were encountered: