-
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
type guard doesn't seem to work on interfaces #1957
Comments
This is working as intended according to the latest spec. From section 4.20 Type Guards:
That said, I'm not aware of the rationale here to do no narrowing in the else clause, it seems like this should work. @ahejlsberg can weigh in. |
Seems related to #1719 |
Thought I had seen this before... don't know why I couldn't find it. Closing this as a dupe then. |
Ok, this limits the usage of type guard to primitives only. Type guard
should be replaced by
We are working with interfaces a lot, so I hope there will be a solution soon. |
@danquirk, this may not be a duplicate. It's not about narrowing in the else block. |
Thanks @NoelAbrahams. I just tried the example: function(x: string|string[]) {
if (x instanceof Array) {
A(x);
else {
B(x);
}
} with the latest |
That would be #1719, the else block isn't getting a narrowed type by design at the moment. |
The type guard
instanceof Array
does not seem to recognize the type of the interface IMessage.The compiler outputs the error message:
I'm using master of today.
Thanks
The text was updated successfully, but these errors were encountered: