tsling raise an error:
Line 1: 'use strict' is unnecessary inside of modules (strict)
this is my code
"use strict";
function Foo() {}
Foo.prototype.sayHello= function () {
console.log("hello!");
}
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = {
Foo: Foo
};
}
how fix this error?
Side note
my code is used both module
and vanilla javascript. I want use "strict mode"
only for vanilla javascript.
maybe i can use
if (typeof module !== 'undefined') {
"use strict";
}
for enabling strict mode
only for vanilla javascript?
Is use strict necessary? The strict mode is no longer required since the release of ES2015, which fixes most of JavaScript's confusing behavior with a more robust syntax. It's just good to know because you might find it in legacy projects that still used it.
Strict Mode. Strict Mode(“use strict”) helps identify common issues (or “bad” parts) and also helps with “securing” JavaScript. In ES5, the Strict Mode is optional but in ES6, it's needed for many ES6 features.
The JavaScript strict mode is a feature in ECMAScript 5. You can enable the strict mode by declaring this in the top of your script/function. 'use strict'; When a JavaScript engine sees this directive, it will start to interpret the code in a special mode.
Benefits of Strict Mode The use of strict mode: helps to write a cleaner code. changes previously accepted silent errors (bad syntax) into real errors and throws an error message. makes it easier to write "secure" JavaScript.
Remove 'use strict'
. As the error mentions, it's unnecessary. Modules are expected to execute in strict mode. Compilers will add it for you when you export the module into a script for non-module consumption (i.e. UMD/CJS). See --alwaysStrict
option for TS.
ES6 modules are always in strict mode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With