-
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
Legitimate type assertion causes compiler error #7598
Comments
@philthy256 check which version of
@paulvanbrenk is that expected? |
No. The type of |
Yeah, narrowing only occurs for types that are ObjectType, Union or TypeParameter. It doesn't happen for ThisType. |
Where did you get that NuGet package from, we haven't released any 2.0 bits yet. And can you make a screenshot where you see that version? |
Thanks for all the responses guys. @paulvanbrenk I grabbed it from the nuget package manager, but now I look more closely I wonder if the empty "author" field is suspect! Screenshot attached. Installing the package was a vain attempt to make the described problem to go away, mind. Before that I was using the Typescript that came built into Visual Studio Community 2015 (v14.0.24720.00 Update 1). Looking at Tools|Extensions and updates in Visual Studio, it looks like I've actually got Typescript 1.7.6.0. If I remove the dubious looking v2.0 nuget package the compilation error remains, though. |
@DanielRosenwasser I tried "where tsc" in a regular command line window and a Visual Studio Developer Command Prompt and got "INFO: Could not find files for the given pattern(s)." back in both cases. I'm afraid I'm not too hot on anything Typescript via the command line. I just tend to do everything via Visual Studio! Sorry! |
@philthy256 Yes. you'll want to use the Microsoft.TypeScript.Compiler, or in the case of VS, the MSBuild package. |
@paulvanbrenk Thanks Paul. The Microsoft.TypeScript.Compiler nuget package seems a whole lot more sensible. Can now confirm this issue shows on v1.8.6. |
this should be fixed in latest as part of #7140 |
Awesome - thank you! |
Typscript version
Nuget package manager in Visual Studio reports it as v2.0.0
"tsc --v" reports it as v1.0.0.0
Code
Expected behavior:
Should compile without issue. I expect to be able to assert that "this" is of type Dog, because it can be. I also note that "woof" is referred to as a property in the error message, when perhaps it should be referred to as a method.
Obviously in this case, it would make sense to override Animal.speak() in Dog rather than check the type in Animal.speak(), but sometimes it makes sense to check the type of "this" explicitly, perhaps because you want to perform some action in the middle of a method in the base class, between various other actions, and overriding doesn't make that possible.
In any case, good practice or bad practice, it's valid code and should compile.
Actual behavior:
Error TS2352 Neither type 'this' nor type 'Dog' is assignable to the other.
Type 'Animal' is not assignable to type 'Dog'.
Property 'woof' is missing in type 'Animal'.
The text was updated successfully, but these errors were encountered: