javascript - Typescript and Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outs

I have a typescript file containing a class definition:if (window.console == null) {(<any>window)

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 pilation 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

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 pilation 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

Share Improve this question asked Nov 10, 2015 at 15:51 o..oo..o 1,9317 gold badges36 silver badges65 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

It's because it's piling 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 pilation target from ES6 to ES5. If you are using tsconfig.json, change "target": "es6" to "target": "es5". Doing that will...pile 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:

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743624310a4480287.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信