The void function can only be called using the "new" keyword

I am trying to use the Foundation Reveal plugin in my Typescript code as follows (modified for readability):

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

and during compilation I get the following error (in the end, it compiles and works anyway):

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

How do I record it?

Typescript Playground - Code Illustrating the Problem

+4
source share
3 answers

Based on the interface:

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

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

So to fix:

var popup = Foundation.Reveal($('#element'));
+3
source

typescript, ( ).

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

:

function User2(name) {
    if (this instanceof User2) {
        this.name = name;
    }
    else {
        return new (User2 as any)(name);
    }
}
+2

I think you want this:

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

Which allows you to do what works without TypeScript errors and without compromising type safety!

const popup = new Foundation.Reveal($('#element'));
0
source

All Articles