TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Suggestion: Extension methods

Open RyanCavanaugh opened this issue 12 years ago • 170 comments

Allow a declarative way of adding members to an existing type's prototype

Example:


class Shape {
 // ...
}

/* ...elsewhere...*/
extension class Shape { // Syntax??
    getArea() { return /* ... */; }
}

var x = new Shape();
console.log(x.getArea()); // OK

RyanCavanaugh avatar Jul 15 '14 16:07 RyanCavanaugh

+1

roelvanlisdonk avatar Jun 05 '15 08:06 roelvanlisdonk

How could someone contribute for the extension classes in Typescript ?

Kavignon avatar Jun 15 '15 17:06 Kavignon

Would be great if we could extend enums with this. Something like

enum View{
   Edit,
   Detail
}

View.Edit.asString()

vs what we have to do now:

View[View.Edit]

The first one is a bit more readable and flows nicer than the latter.

bchavez avatar Jun 28 '15 22:06 bchavez

Note that you can add to an enum using module (or namespace):

enum E {
    A, B
}
module E {
    export function toString(e: E): string {
        return E[e];
    }
}

var x = E.toString(E.A);

RyanCavanaugh avatar Jun 28 '15 22:06 RyanCavanaugh

#3508 looks like the compromise solution. Avoids backwardness, doesn't mess with prototypes and doesn't rely on type information. The only downside is it has a funny-looking syntax. I'd prefer -> for call.

So this:

o.my()::Extension.method().rocks();
// or preferably this
o.my()->Extension.method().rocks();

desugars to:

Extension.method.prototype.call(o.my()).rocks();

There's a possible ambiguity if there were extension properties though, or methods which return extension methods. There's also the issue that using a different syntax instead of dot hurts the motivating case which is I think is transparency.

benliddicott avatar Jul 14 '15 21:07 benliddicott

:+1:

Zorgatone avatar Sep 28 '15 08:09 Zorgatone

It seems implement in TypeScript 1.7

class Shape {
 // ...
}

interface Shape {
    getArea()
}


var x = new Shape();
console.log(x.getArea()); // OK

Should we close this issue ?

WanderWang avatar Oct 30 '15 01:10 WanderWang

That does not create a new method

Zorgatone avatar Oct 30 '15 06:10 Zorgatone

Well this is exactly what I was looking for, so for me a YES, on closing this issue.

roelvanlisdonk avatar Oct 30 '15 06:10 roelvanlisdonk

@Zorgatone

you can create a new method with this way

class Shape {
 // ...
}

interface Shape {
    getArea()
}

Shape.prototype.getArea = function(){
}


var x = new Shape();
console.log(x.getArea()); // OK

WanderWang avatar Oct 30 '15 12:10 WanderWang

Oh, that's with prototype. I wasn't considering that.

I tried to do something similar, but without the interface trick :+1:

Zorgatone avatar Oct 30 '15 14:10 Zorgatone

@WanderWang @Zorgatone You can't do that, I tried doing that in separate files using the es6 module syntax and got tons of errors. I think it only works with the legacy external modules.

niemyjski avatar Oct 30 '15 15:10 niemyjski

Uhm then keep the issue open :D

Zorgatone avatar Oct 30 '15 15:10 Zorgatone

-1 Humans cannot understand code flow using extension methods. Extension should be declarative feature.

falsandtru avatar Nov 26 '15 18:11 falsandtru

The problem with doing this is that (as hopefully we’ve all dealt with), is it could pollute the existing code in a more complex environment. “Extensions” would simply be compiler sugar for writing utility methods that would pass the ‘this’ along.

From: Wander Wang [mailto:[email protected]] Sent: Friday, October 30, 2015 5:52 AM To: Microsoft/TypeScript Cc: electricessence Subject: Re: [TypeScript] Suggestion: Extension methods (#9)

@Zorgatone https://github.com/Zorgatone

you can create a new method with this way

class Shape { // ... }

interface Shape { getArea() }

Shape.prototype.getArea = function(){ }

var x = new Shape(); console.log(x.getArea()); // OK

— Reply to this email directly or view it on GitHub https://github.com/Microsoft/TypeScript/issues/9#issuecomment-152515414 .Image removed by sender.

electricessence avatar Dec 12 '15 03:12 electricessence

-1 Humans cannot understand code flow using extension methods.

(-1'ing everything is not really helping)

There are countless real world use-cases where extension methods are proven to be useful.

If you are unsure about the significance of extension methods, take a look at C#.

If you don't like the way it is proposed to be implemented, please provide an alternate approach.

Extension should be declarative feature.

Yes and this is in the very first line of this proposal.

ghost avatar Dec 12 '15 06:12 ghost

I don't think that it is declarative because we cannot get the whole story. We must find pieces of a class.

falsandtru avatar Dec 12 '15 06:12 falsandtru

If you are building the library yourself, then you should be able to add the additional methods in the class. If you are working with someone's else library (which is most likely documented) then you can add extension methods elsewhere in your code without touching vendor/** code. Finding the pieces to form the 'whole story' is a non-issue especially when editors are there to assist you.

Splitting the code into separate files vs. dumping everything in one place, both have pros and cons. Giving this choice to the developer however they want to consume the feature (or avoid it) would be the best strategy from TypeScript team's standpoint. Any feature of any language can be misused.

ghost avatar Dec 12 '15 08:12 ghost

:+1:

xfoxfu avatar Dec 18 '15 12:12 xfoxfu

:+1:

I would suggest that Extension is only useful for telling the compiler there is an existing member for the existing class, so the code:

class A {}
extension class A {
  func() : void =>  {
  }
}

could be just compiled to:

class A{}
A.prototype.func = () : void => {
}

but such Extension is of great use to intellisense.

xfoxfu avatar Dec 18 '15 12:12 xfoxfu