-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Completion does not work with instanceof operator (typescript) #6527
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
@ltearno it depends on what the type of |
Thanks I get it ! Maybe there is something to add in the documentation However, from the developper point of view, having guarded a code against "instanceof" guarantees that the variable is of the correct type. So what i don't get is WHY this behavior ? I am definitely missing something i guess... But wouldn't it be great if that code worked ? Here is a demo piece of code :
I have also tested that with user defined type-guards functions and the same behavior applies. But with user defined type-guards, the input parameter is declared of type 'any' so i really expect it to work for the Here is the code with the user defined type-guard function :
|
The idea is that with That sounds ridiculous to most people at first, but the idea is that there's code out there like the following: function f(x) {
if (x instanceof Object) {
// assume x is a well-known or "expected" type of object that's getting passed around
}
else if (typeof x === "number") {
// x is a number
}
else {
// assume x is a string or something
}
} You'll see this type of code in TypeScript that predates union types and TypeScript that's been ported over from JavaScript. If we narrowed from We are considering changing the behavior for |
Thank you, i completely get the point, that's perfect. Le mar. 19 janv. 2016 à 19:30, Daniel Rosenwasser notifications@github.com
|
From @ltearno on January 15, 2016 13:57
With a typescript source file, when using the type guarded type inference, the auto completion does not work...
An example :
Thanks !
Copied from original issue: microsoft/vscode#2046
The text was updated successfully, but these errors were encountered: