Typescript and Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

18,911

It's because it's compiling to ES6 and the browser is requiring that block-scoped declarations be used in strict mode.

You can fix this by using strict mode. To do that add...

"use strict";

...to the top of every file.

However, I think you probably want to change the compilation target from ES6 to ES5. If you are using tsconfig.json, change "target": "es6" to "target": "es5". Doing that will...compile to ES5...and so block-scoped declarations will be changed appropriately so "use strict"; will not be required. Additionally, more browsers will support your code. Right now runtime ES6 support is still not widespread.

Note that if you are not using tsconfig.json, you might have to change the target in the project properties' typescript build tab as shown here:

ECMAScript version change

Share:
18,911
o..o
Author by

o..o

Updated on June 28, 2022

Comments

  • o..o
    o..o almost 2 years

    I have a typescript file containing a class definition:

    if (window.console == null) {
        (<any>window).console = {
                error: function (a) {
            },
                log: function (a) {
            }
        };
    }
    
    class SendMessage {
        //.....
    }
    

    After the compilation to javascript (by VS2015), I get the error on the line with the class definition:

    Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    

    I have found that I have to use the strict mode. But why and how can I use it in typescript?

    Thanks

  • sanjeev
    sanjeev over 8 years
    I am also getting the same error. If I use target: es5, i wont be able to use async/await. Is there any workaround for this.
  • David Sherret
    David Sherret over 8 years
    @sanjeev add "use strict"; to the top of the files as shown in the answer or wait for TypeScript 2.0 when ES5 targeting with async/await will be available.
  • User
    User about 8 years
    What does use strict mean?
  • David Sherret
    David Sherret about 8 years