See #9449 and #10755 for reference.
The ts code base is not modules, so it is not implicitly parsed in strict mode. we would like it to be. options either 1. put "use strict" at the top of every file, and make the emitter remove the duplicate ones when it merges the output. or 2. add a compiler flag that would force the compiler to parse the files in strict mode.
Hi,
for a software product at work, we currently use a fork of TypeScript that has a --forceStrictMode compiler option which forces the compiler to parse in strict mode, without affecting the emit. The use case is a similar one:
The use case for this is that some JS Runtimes like Jurassic (which allows to run JavaScripts in a .NET environment) have an option to enable strict mode even if the JS did not declare a strict mode directive, and enabling strict mode slightly improves performance (and changes some semantics). Then, if you present an editor like Monaco to the user where he can edit TypeScripts that are to be run on such a JS Runtime later with strict mode, the editor's TypeScript Language Service can correctly identify syntax errors in strict mode even if the script does not contain the strict mode directive.
Note that enabling this option will not have any impact on the output, so if the TS file did not contain
"use strict";, neither will the JS output file. It will only have an impact on the compiler during syntax check (to emit errors e.g. like using reserved words as identifiers, using thedeleteoperator on identifiers, using old-style octal literals etc).
If this is integrated directly into the TS repo, we could abandon the fork and use the original TS Services lib instead.
Thank you!
:+1: I would really like to see "use strict"; emitted
Accepting PRs for a new flag/setting --alwaysStrict (perhaps open to bikeshedding on this point) which:
@T18970237136 perhaps you could turn your fork into a PR?
Note, that a feature request for the same behavior (compiler option that parses in strict mode and also emits "use strict";) was already declined some time ago: #7209 (Suggestion: Add a "forceStrictMode" compiler option) :wink:
馃憥 write "use strict"; yourself + TSLint if appropriate
@RyanCavanaugh Note that my fork currently only implements the first part: parse source files in strict mode. It does not affect the emit, so it will not emit "use strict"; on file which did not have this directive. (This is because in our use case the "use strict"; was not needed, and unfortunately I have no knowledge about the new transformation-based emitter, and how to ensure "use strict" is always emitted.)
Thanks!
thanks @slawomir!
The new option is --alwaysStrict should be available in typescript@next later tonight.
Most helpful comment
The new option is
--alwaysStrictshould be available intypescript@nextlater tonight.