Skip to content

Legitimate type assertion causes compiler error #7598

Description

@philthy256

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

Activity

  1. DanielRosenwasser commented on Mar 20, 2016

    @DanielRosenwasser
    Member

    Nathan Shively-Sanders (@sandersn) does your work in #7422 partially fix this due to narrowing the constraint?

  2. DanielRosenwasser commented on Mar 20, 2016

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

    Paul van Brenk (@paulvanbrenk) is that expected?

  3. sandersn commented on Mar 21, 2016

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

  4. sandersn commented on Mar 21, 2016

    @sandersn
    Member

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

  5. paulvanbrenk commented on Mar 21, 2016

    @paulvanbrenk
    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?

  6. philthy256 commented on Mar 21, 2016

    @philthy256
    Author

    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.

    typescript 2 0

  7. philthy256 commented on Mar 21, 2016

    @philthy256
    Author

    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!

  8. paulvanbrenk commented on Mar 21, 2016

    @paulvanbrenk
    Contributor

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

  9. philthy256 commented on Mar 21, 2016

    @philthy256
    Author

    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.

  10. mhegazy commented on Mar 23, 2016

    @mhegazy
    Contributor

    this should be fixed in latest as part of #7140

  11. philthy256 commented on Mar 24, 2016

    @philthy256
    Author

    Awesome - thank you!

  12. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions