You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Suggestion: Add a "forceStrictMode" compiler option #7209
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.
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.
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").
We 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.
Activity
mhegazy commentedon Feb 24, 2016
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.
T18970237136 commentedon Feb 29, 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!
RyanCavanaugh commentedon Mar 8, 2016
We 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.
DavidKDeutsch commentedon Aug 3, 2016
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.