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

Allow this as return type from static methods #8460

Closed
iby opened this issue May 4, 2016 · 1 comment
Closed

Allow this as return type from static methods #8460

iby opened this issue May 4, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@iby
Copy link

iby commented May 4, 2016

TypeScript allows to return this from instance methods, which is to say the least, is great. It would be even better if it allowed doing the same with static methods. The rationale is consistency and feature.

Line public static construct():this below will report error TS2526: A 'this' type is available only in a non-static member of a class or interface., the line below uses this inside static method to create a new instance of the class, the line below that also uses this to return static property. The error message is misleading, this can be and should be used inside static methods whenever appropriate.

class Foo {
    public static x:any;
    protected y:any;
    public static construct():this {
        return new this();
    }
    public static getX():any {
        return this.x;
    }
    public getY():any{
        return this.y;
    }
    public setY(value:any):this{
        this.y = value;
        return this;
    }
}

I'd like to inherit Foo and use construct on child classes, the only way I can do this by specifying Foo as return type, however, this will require to cast the result to Bar and Baz whenever called from those classes – inconvenient and inconsistent with instance method behaviour.

This seems to be common sense, since this already done for instance methods, shouldn't be much pain doing this for static methods either? What are your thoughts?

@mhegazy
Copy link
Contributor

mhegazy commented May 4, 2016

Duplicate of #5863

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 4, 2016
@mhegazy mhegazy closed this as completed May 4, 2016
@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

2 participants