Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
189
rated 0 times [  192] [ 3]  / answers: 1 / hits: 19216  / 9 Years ago, tue, november 10, 2015, 12:00:00

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


More From » typescript

 Answers
3

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


[#64449] Saturday, November 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
benitoh

Total Points: 150
Total Questions: 113
Total Answers: 104

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
benitoh questions
Sun, Mar 21, 21, 00:00, 3 Years ago
Mon, May 13, 19, 00:00, 5 Years ago
;