Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only void function can be called with the "new" keyword

I'm trying to use the Foundation's Reveal plugin in my Typescript code in following way (altered for readability):

var popup = new Foundation.Reveal($('#element'));

and I'm getting following error during compilation (in the end it does compile and work anyway):

TS2350: Only a void function can be called with the 'new' keyword.

How should I write it then?

Typescript Playground - code illustrating the problem

like image 353
Wiktor Avatar asked Jun 13 '16 12:06

Wiktor


2 Answers

I find a way to shut the typescript compiler up, but this will give up the type check(Do it with your caution).

var popup = new (Foundation.Reveal as any)($('#element'));

More example:

function User2(name) {
    if (this instanceof User2) {
        this.name = name;
    }
    else {
        return new (User2 as any)(name);
    }
}
like image 125
aristotll Avatar answered Sep 19 '22 04:09

aristotll


Based on the interface:

interface FoundationSitesStatic {
    Reveal(element:Object, options?:IRevealOptions): Reveal;
}

you cannot call it with the new operator (used to call a constructor).

So fix:

var popup = Foundation.Reveal($('#element'));
like image 28
basarat Avatar answered Sep 20 '22 04:09

basarat