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

Intellisense/TSServer bug with self reference of class in inheritance #3170

Closed
loucyx opened this issue May 14, 2015 · 6 comments
Closed

Intellisense/TSServer bug with self reference of class in inheritance #3170

loucyx opened this issue May 14, 2015 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@loucyx
Copy link

loucyx commented May 14, 2015

Given the following code:

class Parent {
    public doStuff() {
        /* stuff */
        return this;
    }
}

class Child extends Parent {
    public doOtherStuff() {
        /* other stuff */
        return this;
    }
}

var child = new Child();
child.doStuff().doOtherStuff(); // Error: doOtherStuff doesn't exist on class Parent (but this is class Child)

The last method returns the "this" from Parent (the doOtherStuff method doesn't exists), instead of Child, so extend becomes really anoying, because I have to workarounds (both bad):

Workaround 1:

class Parent<Generic extends Parent<any>> {
    public doStuff(): Generic {
        /* stuff */
        return <Generic>this;
    }
}

class Child extends Parent<Child> {
    public doOtherStuff() {
        /* other stuff */
        return this;
    }
}

var child = new Child();
child.doStuff().doOtherStuff(); // OK

With this one, the result is the spected one, but the code is repetitive.

Workaround 2:

class Parent {
    public doStuff() {
        /* stuff */
        return this;
    }
}

class Child extends Parent {
    public doStuff() {
        super.doStuff();
        /* stuff */
        return this;
    }
    public doOtherStuff() {
        /* other stuff */
        return this;
    }
}

var child = new Child();
child.doStuff().doOtherStuff(); // OK

With this one is even worst, because I need to write every inherited function again to recover the "this" of the child.

Is this a bug or a feature?. If not a bug, then could I ask for an explanation for this and a better workaround if there is one?

Thanks!

Edit 1:
I just tested the code in Babel, and in io.js, and it works as excpected, so I think this is a TypeScript bug... Check it here

Edit 2:
I use the TypeScript compiled code, and it works as excpected, so it's a problem with tsserver/intellisense.

@loucyx loucyx changed the title Self reference of class lost with inheritance Intellisense/TSServer bug with self reference of class in inheritance May 14, 2015
@DanielRosenwasser
Copy link
Member

I'm not sure if I understand correctly; is the issue you're having that without your workarounds, child.doStuff() returns type Parent instead of Child?

@DanielRosenwasser
Copy link
Member

After looking more closely, I believe it is. If so, this is not a bug in the language service, this is the way the language works.

Still, we understand that this can be undesirable and issue #285 tracks a solution so that you can better express the intent of your code.

@CyrusNajmabadi
Copy link
Contributor

The problem with the language currently is that there is no way to say "the result of this call is an instance of the value you called it on." This makes chaining and fluent APIs difficult to write in the presence of subclassing. This limitation is pretty common in many languages, though I'd definitely like to see some improvement here as well.

@danquirk
Copy link
Member

See #513

@danquirk danquirk added the Duplicate An existing issue was already created label May 15, 2015
@loucyx
Copy link
Author

loucyx commented May 15, 2015

@DanielRosenwasser I edited the code to make it clear, the issue is that the Parent class is returned instead of Child.

@CyrusNajmabadi Agree, this is a bummer. I hope the TypeScript team can fix this, because is getting annoying pretty fast.

@danquirk great, I didn't found that early. Maybe this issue is related to #15 .... thanks!

@DanielRosenwasser
Copy link
Member

As I've pointed out above #285 is indeed what you're looking for, with related issues linked to in #513.

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants