Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript error in Typescript generated JS in IE11

I am getting following error from a simple module/class in typescript when I load page in IE11. The error does not happen in other browsers like Edge and Chrome.

JavaScript critical error at line 4, column 5 in clock.js SCRIPT1002: Syntax error

Here is TS code.

module DateTime {
    export class TestMe {
        private timeNow:Date;
        constructor() {
            alert("Hello");
        }
    }
}

And here is JS code generated from it.

var DateTime;
(function (DateTime) {
    class TestMe {
        constructor() {
            alert("Hello");
        }
    }
    DateTime.TestMe = TestMe;
})(DateTime || (DateTime = {}));

This is how this is getting invoked on page.

<script type="text/javascript">
            $(document)
                .ready(function() {
                        var testIt = new DateTime.TestMe();
                    }
                );
    </script>

From the debugger I can see that it is not liking "class" keyword in JS code. Page does not even get to create instance of "TestMe" because syntax error in clock.js does not let that file load. Is there anything that I need to include for it to work in IE11? I have tried to incluse es6 shim as well but same issue.

Thanks for any input on this issue.

like image 237
ByteBlocks Avatar asked Apr 19 '16 16:04

ByteBlocks


People also ask

Does ie11 support TypeScript?

IE 11 does not support the class keyword and language features, according to the compatibility table. You can force the Typescript compiler to output code that will be compatible with an older version of JavaScript using the --target option or equivalent in your build.

Does ie11 support JavaScript?

Internet Explorer 11 doesn't support JavaScript versions later than ES5. If you want to use the syntax and features of ECMAScript 2015 or later, or TypeScript, you have two options as described in this article. You can also combine these two techniques.

Does browsers can execute TypeScript directly?

TypeScript cannot be run or understood in any browser. So, TypeScript is compiled to JavaScript (which browsers can understand).

Can you write TypeScript in JavaScript?

By understanding how JavaScript works, TypeScript can build a type-system that accepts JavaScript code but has types. This offers a type-system without needing to add extra characters to make types explicit in your code. That's how TypeScript knows that helloWorld is a string in the above example.


1 Answers

IE 11 does not support the class keyword and language features, according to the compatibility table.

You can force the Typescript compiler to output code that will be compatible with an older version of JavaScript using the --target option or equivalent in your build. Otherwise, you'll need to run the TS output through another transpiler (such as Babel) to produce ES5 that will run under IE.

like image 160
ssube Avatar answered Sep 24 '22 11:09

ssube