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
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
Share Improve this question asked Jun 13, 2016 at 12:09 WiktorWiktor 3,0595 gold badges18 silver badges23 bronze badges 3 |3 Answers
Reset to default 11I 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);
}
}
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'));
I think you want this:
interface FoundationSitesStatic {
Reveal: new (element: Object, options?: IRevealOptions) => Reveal;
}
Which lets you do what works without TypeScript errors and without sacrificing type safety!
const popup = new Foundation.Reveal($('#element'));
var popup = Foundation.Reveal($('#element'))
(that is, without thenew
) – Nitzan Tomer Commented Jun 13, 2016 at 13:52