-
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 bug with condition #20393
Comments
Can you explain why you think this is a bug? I can see why you might be surprised that the narrowing of In any case you can work around this yourself: function abc(foo: Foo) {
// Arr is a specifically constructor for Array<{}>
const Arr: { new(): Array<{}> } = Array;
// foo.bar[0] is now typed as {}, and the expected error appears
abc(foo.bar instanceof Arr ? foo.bar[0] : foo.bar); // {} isn't assignable to Foo
} |
I don't follow you, instanceof Array don't narrow to any[] const a:number[] = [];
if (a instanceof Array) {
a[0].foo = 1; // error foo doesn't exist on number
} |
Not if it already knows that it's possibly narrowing an array, I guess. Someone more familiar with the control flow analysis will have to say how that works... maybe #9999 is related? But in your case, Are you suggesting that this is a bug? If so, what do you want it to do instead? |
Here's the behavior I'm seeing: function abc(foo: Foo) {
if (foo.bar instanceof Array) {
abc(foo.bar[0]); // Ok, type of foo.bar narrowed to Bar & any[]
}
else {
abc(foo.bar); // Error, foo.bar is of type Bar
}
} In the Best I can tell this all is working as intended. |
@ahejlsberg, Sorry, it is incorrect example function abc(foo: Foo) {
abc(foo.bar instanceof Array ? foo.bar[0] : foo.bar); // no error
} This expression So, this is not bug, this is feature :( |
So, I think |
TypeScript Version: 2.6.1
Code
The text was updated successfully, but these errors were encountered: