Terkosh Terkosh - 2 months ago 5x
TypeScript Question

What kind of javascript syntax is this: "var XX = (function (){...})();" and how can I use it with TypeScript

I found this in the PDFJS viewer sample:

var XX = (function XXClosure() {
function XX(options) {
XX.prototype = {
myMethod1: function(){...},
myMethod2: function(){...},
return XX;

I create a construct like this, without any idea what it is, and used it in a javascript project in this way:

var myXX = new XX(myOptions);

This works very well.

Now i try to use this construct in a TypeScript project:

declare XX:any
import myJavascript.js;
var myXX = new XX(myOptions);

and i get at runtime the error: "XX is not a constructor"

Don't ask me why i make it in this way, may be there are others, I copied this from PDFJS viewer.js, it worked and i never asked why.

But now, in the typescript project, it should work too.Has someone a solution for this?


First is IIFE

For TypeScript, you can use class (documentation)