Suggestion: Extension methods
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
+1
How could someone contribute for the extension classes in Typescript ?
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.
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);
#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.
:+1:
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 ?
That does not create a new method
Well this is exactly what I was looking for, so for me a YES, on closing this issue.
@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
Oh, that's with prototype. I wasn't considering that.
I tried to do something similar, but without the interface trick :+1:
@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.
Uhm then keep the issue open :D
-1 Humans cannot understand code flow using extension methods. Extension should be declarative feature.
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.
-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.
I don't think that it is declarative because we cannot get the whole story. We must find pieces of a class.
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.
:+1:
:+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.