Skip to content

Constructor chain does not contain code added by class decorator #10840

Description

@ahagelstein

The example code decorates the class "TestClass" with a kind of inheritence (with the function "extender").
But this extension is not used if the instance of "TestClass" is created by a static method from the class "TestClass" (see console outputs).

export function startTest() {
    console.debug("test 1");
    new TestClass();

    console.debug("test 2");
    TestClass.createInstance();

    console.debug("end test");
}

function extender(d: any, b: any) {
    for (const p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}

export function testDecorator(): ClassDecorator {

    return <TFunction extends Function>(target: TFunction): TFunction => {

        const func = <any>function (...args: any[]): any {

            console.debug("decorator called");
            target.apply(this, args);
        }

        extender(func, target);

        return func;
    }
}

@testDecorator()
export class TestClass {

    public constructor() {

        console.debug("constructor called");
    }

    public static createInstance() {

        new TestClass();
    }
}

Expected behavior:

with typescript@2.0.6

test 1
decorator called
constructor called
test 2
decorator called
constructor called
end test

Actual behavior:

with typescript@2.1.0-dev.201620161102

test 1
decorator called
constructor called
test 2
constructor called
end test

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions