FG
💻 Software🌐 Web & Full-StackMicrosoft

Suggestion: Extension methods

Freshover 9 years ago
Mar 14, 20260 views
Confidence Score79%
79%

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

Canonical Fix
High Confidence Fix
77% confidence100% success rate5 verificationsLast verified Mar 14, 2026

Solution: Suggestion: Extension methods

Low Risk

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

77

Trust Score

5 verifications

100% success
  1. 1

    While the naive implementation of extension methods is probably like this:

    While the naive implementation of extension methods is probably like this:

    text
    Compiling 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. 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. 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. 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

Worked: 5
Partial: 1
Last verified Mar 14, 2026

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

typescriptcompilersuggestionout-of-scope