13

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?

ar099968
  • 6,963
  • 12
  • 64
  • 127

2 Answers2

17

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.

Joseph
  • 117,725
  • 30
  • 181
  • 234
6

ES6 modules are always in strict mode.

Ginie
  • 86
  • 1
  • 3