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

'this' return type #1435

Closed
jamiewinder opened this issue Dec 10, 2014 · 1 comment
Closed

'this' return type #1435

jamiewinder opened this issue Dec 10, 2014 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@jamiewinder
Copy link

Admittedly I haven't given this as much thought as it may need, but I thought it worth suggesting.

In JavaScript, it's common to return 'this' so the function can be chained. In most cases this can be easily defined:

class Obj
{
    public chainable(): Obj { ...; return this; }
}

However, if I extend the class:

class MegaObj extends Obj
{
    public doThing(): MegaObj { ...; return this; }
}

Then I can do:

var mo: MegaObj = ...
mo.doThing().chainable();

But I can't do...

mo.chainable().doThing();

Because 'chainable' returns 'Obj'. I think it'd be useful to be able to specify that the return type is the same as the object that the method was called on:

class Obj
{
    public chainable(): this  { ...; return this; }
}

class MegaObj extends Obj
{
    public doThing(): this  { ...; return this; }
}

var mo: MegaObj = ...
var mo2 = mo.chainable(); // mo2 is a MegaObj, because mo is a MegaObj
mo2.doThing();  // ok

Any feedback welcome, if only to inform me why this is a bad idea! Thanks.

@danquirk
Copy link
Member

Yep, would be nice, already covered here #513

@danquirk danquirk added the Duplicate An existing issue was already created label Dec 10, 2014
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 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