Repository navigation
Legitimate type assertion causes compiler error #7598
Description
Activity
DanielRosenwasser commented
on Mar 20, 2016 MemberMore actionsNathan Shively-Sanders (@sandersn) does your work in #7422 partially fix this due to narrowing the constraint?
DanielRosenwasser commented
on Mar 20, 2016 MemberMore actions"tsc --v" reports it as v1.0.0.0
philthy256 check which version of
tschas higher priority in your environment'sPATHvariable by typing inwhere tscinto a Command Prompt window.Nuget package manager in Visual Studio reports it as v2.0.0
Paul van Brenk (@paulvanbrenk) is that expected?
sandersn commented
on Mar 21, 2016 MemberMore actionsNo. The type of
thisin a method is determined by code incheckThisExpression:getDeclaredTypeOfSymbol(classSymbol).thisType. I'm pretty sure that this code runs for everythisreference, so narrowing code doesn't even apply. I'll look a little more.sandersn commented
on Mar 21, 2016 MemberMore actionsYeah, narrowing only occurs for types that are ObjectType, Union or TypeParameter. It doesn't happen for ThisType.
paulvanbrenk commented
on Mar 21, 2016 ContributorMore actionsNuget package manager in Visual Studio reports it as v2.0.0
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.
Paul van Brenk (@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.
Daniel Rosenwasser (@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!
paulvanbrenk commented
on Mar 21, 2016 ContributorMore actionsphilthy256 Yes. you'll want to use the Microsoft.TypeScript.Compiler, or in the case of VS, the MSBuild package.
Paul van Brenk (@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.
- addedFixedA PR has been merged for this issueA PR has been merged for this issue
on Mar 23, 2016 this should be fixed in latest as part of #7140
Awesome - thank you!
- locked and limited conversation to collaborators
on Jun 19, 2018

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'.