-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 guards are not working in this case #1595
Comments
Type guards do not affect variables of type @jonathandturner We should probably update the example in the blog post. |
As per conversation off-line yesterday, I think this should continue to give the more specific type. With simple types like 'string' we have a complete picture of what the user should be able to do in the if-block. |
Strong agreement that we should narrow expressions of type |
Thanks!
|
Btw what is the rationale to not also support this in instanceof blocks? |
#1426 has a specific example of the general class of issues with that case. |
While I understand this for doing "instanceof Object", I cannot think of a usecase where I do "instanceof MyClass" where I would not want an error to show up when I use a property that does not exist? |
Also: If I explicitly assign "any" to a type, this is a good indication that I want the type to be any and no errors should show up. But when the type is simply having "any" by default because I did not set a type, you should support type guards. |
@bpasero - If you're a subclass of MyClass, then instanceof the parent class will be true. This means, we can't say for sure that any property doesn't exist, because it may exist on the subclass. The net result is that we may error on perfectly valid, and correct, code. We're trying to avoid that with type guards. |
I disagree. If I check of instance for a super class of my type, I should
|
I am using the following example that I took directly from the blog post (http://blogs.msdn.com/b/typescript/archive/2014/11/18/what-s-new-in-the-typescript-type-system.aspx)
I am not seeing the error or intellisense for the console.log statement.
Are type guards actually supported in this case or only for union types?
The text was updated successfully, but these errors were encountered: