-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
instanceof
should not exclude possible structural type
#17344
Comments
See #202. Currently, there are no nominal types. |
@andy-ms @RyanCavanaugh It's about the |
instanceof
should not exclude possible structural type
There is no such thing as a structurally typed I would recommend that you simply not use the |
@aluanhaddad Even if TypeScript handles all types as structural types, the runtime check done by I'm not asking for nominal types in TypeScript here, just highlighting the fact that this problem could cause real issues when dealing with libraries that expect nominal types: You could pass a structurally identical object to a function that expects a nominal type (performs an If function calls don't make any difference between nominal or structural type, |
I'm saying it's not a bug. If an API wants to do a pseudo nominal check for some reason, then that API will be a pain to work with but I can work around it by setting the These kinds of checks don't even make sense because they don't tell you anything useful about the so-called subclasses because they don't enforce an interface. Everything about a base class, it's subclasses, and even its heritage is mutable, making it a fairly arbitrary criteria in the first place. People keep trying to use JavaScript classes as if they were writing in a language or like C# or Java but it's a pointless exercise. The functions that actually would be very useful to derive from, Now I realize you may be consuming these APIs, not creating them, but if you are writing APIs that perform such checks please stop wasting people's time by trying to make them conform to a contract that isn't a contract. |
This is not a bug about nominal types (thus not a dupe). @ahejlsberg has more context on this, but see #1719 for what I believe was the original discussion. |
Sorry that comment got amended a few times, I've been trying to dig up the context and match changes in behavior. |
#1719 (comment) really outlines why this works the way it does. TL;DR: While structural matches on class types exist, in practice anyone using |
TypeScript Version: 2.4.2
I was playing with TypeScript, and faced this issue, where I can pass a structural type as an object nominally typed.
But using
instanceof
as a type guard makes TypeScript believe it could not be an Animal anymore.Code
So
instanceof
should not work as a type guard anymore (or at least not remove the type as a possibility from the other block), or structural typing should not be permitted with nominal types.The text was updated successfully, but these errors were encountered: