Skip to content
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

Closed
philthy256 opened this issue Mar 20, 2016 · 11 comments
Closed

Legitimate type assertion causes compiler error #7598

philthy256 opened this issue Mar 20, 2016 · 11 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@philthy256
Copy link

Typscript version
Nuget package manager in Visual Studio reports it as v2.0.0
"tsc --v" reports it as v1.0.0.0

Code

class Animal {

    public speak() {
        if (this instanceof Dog) {
            var dog: Dog = <Dog>this;
            dog.woof();
        }
    }
}

class Dog extends Animal {
    public woof() {}
}

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

@DanielRosenwasser
Copy link
Member

@sandersn does your work in #7422 partially fix this due to narrowing the constraint?

@DanielRosenwasser
Copy link
Member

"tsc --v" reports it as v1.0.0.0

@philthy256 check which version of tsc has higher priority in your environment's PATH variable by typing in where tsc into a Command Prompt window.

Nuget package manager in Visual Studio reports it as v2.0.0

@paulvanbrenk is that expected?

@sandersn
Copy link
Member

No. The type of this in a method is determined by code in checkThisExpression: getDeclaredTypeOfSymbol(classSymbol).thisType. I'm pretty sure that this code runs for every this reference, so narrowing code doesn't even apply. I'll look a little more.

@sandersn
Copy link
Member

Yeah, narrowing only occurs for types that are ObjectType, Union or TypeParameter. It doesn't happen for ThisType.

@sandersn sandersn added the Bug A bug in TypeScript label Mar 21, 2016
@paulvanbrenk
Copy link
Contributor

@philthy256

Nuget 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?

@philthy256
Copy link
Author

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.

typescript 2 0

@philthy256
Copy link
Author

@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
Copy link
Contributor

@philthy256 Yes. you'll want to use the Microsoft.TypeScript.Compiler, or in the case of VS, the MSBuild package.

@philthy256
Copy link
Author

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

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Mar 23, 2016
@mhegazy mhegazy added this to the TypeScript 2.0 milestone Mar 23, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Mar 23, 2016

this should be fixed in latest as part of #7140

@mhegazy mhegazy closed this as completed Mar 23, 2016
@philthy256
Copy link
Author

Awesome - thank you!

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

6 participants