JavaScript Decorator on Class constructor
<p>I'm trying to add some properties in class instances (like a Plugin system).
For that, I followed this <a href="https://stackoverflow.com/questions/34411546/how-to-properly-wrap-constructors-with-decorators-in-typescript">example</a> to do that with a Class Decorator:</p>

<pre><code>function testDecorator(target:any) {
    // save a reference to the original constructor
    var original = target;

    // the new constructor behaviour
    var f : any = function (...args: any[]) {
        console.log("New: " + original.name); 
        return original.apply(this, args)
    }

    // copy prototype so intanceof operator still works
    f.prototype = original.prototype;

    // return new constructor (will override original)
    return f;
}

class AbstractClass {
    constructor() {
        console.log('Init AbstractClass');
    }
}

@testDecorator
class AClass extends AbstractClass {

    constructor() {
        super();
        console.log('Init AClass');
    }

    foo() {
        console.log('Launch foo');
    }

}

class BClass extends AClass {
    constructor() {
        super();
        console.log('Init BClass');
    }

    bar() {
        console.log('Launch bar');
    }
}

var bInstance = new BClass();

bInstance.foo();
bInstance.bar();
</code></pre>

<p>But it doesn't work: as target is a class, I get this error:</p>

<pre><code>return original.apply(this, args);
                       ^
TypeError: Class constructor AClass cannot be invoked without 'new'
</code></pre>

<p>I understand that I cannot do that because of Class constructor (not prototype function "constructor").</p>

<p>So I tried to adapt this code to: </p>

<pre><code>function testDecorator(target:any) {
    // save a reference to the original constructor
    var original = target;

    // the new constructor behaviour
    var f : any = function (...args: any[]) {
        console.log("New: " + original.name); 
        return new original(...args)
    }

    // copy prototype so instanceof operator still works
    f.prototype = original.prototype;

    // return new constructor (will override original)
    return f;
}

class AbstractClass {
    constructor() {
        console.log('Init AbstractClass');
    }
}

@testDecorator
class AClass extends AbstractClass {

    constructor() {
        super();
        console.log('Init AClass');
    }

    foo() {
        console.log('Launch foo');
    }

}

class BClass extends AClass {
    constructor() {
        super();
        console.log('Init BClass');
    }

    bar() {
        console.log('Launch bar');
    }
}

var bInstance = new BClass();

bInstance.foo();

console.log(bInstance instanceof AClass);// true
console.log(bInstance instanceof BClass);// false

bInstance.bar();
</code></pre>

<p>With that, bInstance is not an instance of BClass anymore. So it doesn't work also. So I get this error:</p>

<pre><code>bInstance.bar();
          ^
TypeError: bInstance.bar is not a function
</code></pre>

<p>Any suggestion?</p>

<p>Thanks.</p>