镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Suggestion: Add a "forceStrictMode" compiler option #7209

Description

@T18970237136

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!

Activity

  1. mhegazy commented on Feb 24, 2016

    @mhegazy
    Contributor

    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.

  2. T18970237136 commented on Feb 29, 2016

    @T18970237136
    ContributorAuthor

    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!

  3. added
    Too ComplexAn issue which adding support for may be too complex for the value it adds
    and removed on Mar 8, 2016
  4. RyanCavanaugh commented on Mar 8, 2016

    @RyanCavanaugh
    Member

    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.

  5. DavidKDeutsch commented on Aug 3, 2016

    @DavidKDeutsch

    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.

  6. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it adds

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions