Suggestion: Extension methods
Problem
Allow a declarative way of adding members to an existing type's prototype Example: [code block]
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: Suggestion: Extension methods
While the naive implementation of extension methods is probably like this: [code block] Compiling to: [code block] I think due to first-class functions in JS there is a problem with this approach: [code block] If extension methods are to be implemented, there would probably have to be restirictions on accessing/changing their properties. _Considering "class" syntax_ [code block] In my opi
Trust Score
5 verifications
- 1
While the naive implementation of extension methods is probably like this:
While the naive implementation of extension methods is probably like this:
textCompiling to: ``` javascript // js var _Shape = { getArea:function() { return /* ... */; } } console.log(_Shape.getArea.call(x)); ``` I think due to first-class functions in JS there is a problem with this approach: ``` typescript extension class Shape { // Syntax?? getArea() { return /* ... */; } } var x = new Shape(); var y = new Shape(); console.log(x.getArea); // Function or undefined? x.getArea.foo = "bar"; console.log(y.getArea.foo); // "bar" or undefined? ``` If extension methods are to be implemented, there would probably have to be restirictions on accessing/changing their properties. _Considering "class" syntax_ ``` typescript extension class Shape { // Syntax?? getArea() { return /* ... */; } } - 2
I think due to first-class functions in JS there is a problem with this approach
I think due to first-class functions in JS there is a problem with this approach:
- 3
If extension methods are to be implemented, there would probably have to be rest
If extension methods are to be implemented, there would probably have to be restirictions on accessing/changing their properties.
- 4
In my opinion, one of the common uses of extension methods is to make them work
In my opinion, one of the common uses of extension methods is to make them work on interfaces, not just classes, so you could do something along the lines of
Validation
Resolved in microsoft/TypeScript GitHub issue #9. Community reactions: 10 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep