Repository navigation
Suggestion: Add a "forceStrictMode" compiler option #7209
Description
Activity
One thing to note, modules and classes are already parsed in strict mode, in accordance with ES6 spec. Starting with TS 1.8
"use strict"prologue is emitted by default for modules.I am assuming you are not using modules.
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptIn DiscussionNot yet reached consensusNot yet reached consensus
on Feb 24, 2016 Hi Mohamed Hegazy (@mhegazy),
thanks. Yes, we do not use modules. We would like to force strict mode even for .ts files which doesn't contain classes etc.I've tried to add such a "forceStrictMode" compiler option in a fork with this commit. This adds the behavior that would work for us (if the option is enabled, the compiler treats all files in strict mode, and also emits a "use strict" directive for javascript files). However I don't know if this is done correctly since I don't know the internals of TypeScript, or if it is feasible to add this option directly into TypeScript (a TODO would be to check "noImplicitUseStrict" against "forceStrictMode").
What do you think? Thanks!
- addedToo ComplexAn issue which adding support for may be too complex for the value it addsAn issue which adding support for may be too complex for the value it addsand removedIn DiscussionNot yet reached consensusNot yet reached consensus
on Mar 8, 2016 RyanCavanaugh commented
on Mar 8, 2016 MemberMore actionsWe didn't want to add another commandline flag for this. This is pretty easy to enforce with an external tool, at which point the compiler picks up from there with the strict mode checks.
I was going to suggest this as well, but see this has been closed as Too Complex. In any event, if you ever decide to revisit this issue, consider this a +1 vote for this feature.
- locked and limited conversation to collaborators
on Jun 19, 2018
Hi,
first, thanks for all the great work on Typescript!
It would be very nice if the compiler had some
"forceStrictMode"option which, if enabled, treats all TS files in the project as if they had"use strict";before their first statement (so the TS compiler would apply strict mode checking to all TS files even if they do not have the"use strict";statement, and also emit the"use strict";statement in the transpiled JS files).In our project we use TypeScript for a HTML5 App and we want to use strict mode for every file, and such an option would remove the need from writing "use strict" at the top of every file (sometimes we might forget this when adding a new TS file).
We also use Typescript to be able to execute JavaScript on a Server which runs it using Jint interpreter, which allows to enable strict mode for both parsing and executing the JS even if it doesn't declare "use strict". The option would allow us to reflect this behavior to the TypeScript compiler used in an Editor for transpiling the TS scripts to JS which are executed by this interpreter.
What do you think about it?
Thanks!