Skip to content

Arrow functions of a class show up on prototype #20922

@moraneva

Description

@moraneva

TypeScript Version: 2.7.0-dev.20171226

Code

class Test {
    constructor() { }

    public testPrototypeMethod() {

    }

    public testArrowMethod = () => { }
}

console.log(Test.prototype.testArrowMethod); // This should not compile
console.log(Test.prototype.testPrototypeMethod);

Expected behavior:
Test.prototype.testArrowMethod should fail on compilation because it isn't on the classes prototype.

Actual behavior:
Test.prototype.testArrowMethod does not fail on compilation but becomes undefined at runtime.

Reason
The code above compiles into the following js:

var Test = /** @class */ (function () {
    function Test() {
        this.testArrowMethod = function () { };
    }
    Test.prototype.testPrototypeMethod = function () {
    };
    return Test;
}());

As can be seen, the arrow function is actually defined using this instead of prototype on instantiation. This is fine, but testArrowMethod should not be able to be accessed from Test.prototype

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions