Search code examples
javascripttypescriptdurandaltypescript1.8

Durandal SPA issues with typescript


I am updated my durandal SPA application from VS_2012 to VS_2015 with TypeScript 1.8 which will generate JavaScript (ECMA5). I resolved all build errors.But I am unable to fix one typescript error called

"A 'return' statement can only be used within a function body"

I am working on view models. So I need return statement out side of function.

Due to build error I am not able to generate my java-script.

Below is my sample code in Type script:

class typescript1_8{
    constructor(){

    }
}
return new typescript1_8();

Java Script code needs to generate like below:

var typescript1_8 = (function () {
    function typescript1_8() {
    }
    return typescript1_8;
}());
return new typescript1_8();

Note: I need return statement outside of class. It shouldn't throw any error in Type script as mentioned on above.


Solution

  • You cannot just return something from empty space, you can use Self-invoking function

    (function() {
        return new typescript1_8();
    })();