Skip to content

Strict type-checking on a per-file basis "@ts-strict" #28306

Description

@lukewis

Search Terms

strict per file

Suggestion

Currently in the process of converting a moderate-sized javascript project to typescript. Would love to be able to enable strict type-checks on a per-file basis.

Use Cases

Enabling strict mode for the entire project is a daunting task that presents a barrier to adoption (and using typescript without strict mode turned on is like locking the doors on a convertible....it's probably better than nothing, but it's not gonna keep you safe).

Examples

//@ts-strict

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)

Activity

  1. artursvonda commented on Dec 18, 2018

    @artursvonda

    Would love to see this feature implemented as well. We have quite sizeable project and going "all strict" is not an option right now.

  2. lukewis commented on Dec 18, 2018

    @lukewis
    Author

    Worth mentioning.... that I ran across another project (unfortunately I forget which one right now) that had this same need. Their workaround was to use a separate tsconfig file and run TWO builds. The first build was NOT run in strict mode and was responsible for the actual compilation. They would then run a SECOND build using a tsconfig file that whitelisted specific files that were ready to be compiled in strict mode. The second build had noEmit = true, and was used ONLY for type-checking. I still think it's much nicer to have this supported natively, but wanted to at least document the workaround in the meantime.

  3. rene-leanix commented on Jul 10, 2019

    @rene-leanix

    We tried to implement this with "local" tsconfig.json-files extending the main tsconfig.json with the strict compiler option and including only specific files or all files within and below that folder, e.g.

    {
      "extends": "../../tsconfig",
      "compilerOptions": {
        "strict": true
      },
      "include": [
        "./**/*.ts"
      ]
    }

    together with the following npm scripts

    "tsc-local-tsconfigs": "errors=$(find src/app -name tsconfig.json | xargs -I % sh -c 'tsc -p % | grep error'); echo \"$errors\n\"; [ -z \"$errors\" ] || exit 1;",

    The latter we execute on CI to ensure all files specified in local tsconfig.json and their dependencies can be compiled with those settings.
    When viewing one of those files in VS Code, the editor automatically uses the tsconfig.json file closest to that file that has matching include/exclude patterns, so errors are shown immediately.

  4. gerich-home commented on Aug 20, 2020

    @gerich-home

    Another example when it can be useful.
    I came to a project where TypeScript has been adopted but I found that the team disabled one of strict checks (say "strictFunctionTypes": false,).
    The reason (if I set it to true) turns out to be in bad external typings that were used in another (relatively small) part of project. So to avoid this check in a certain place of a large project and to do not introduce castings / any type and etc. they disabled strict check at all.
    A better approach would be to disable/enable on per file/per directory basis.
    Actually tsconfig has references field, but it involves changes in how the project build procedure works AFAIK (it can be undesirable). Maybe a similar field should be introduced, but that specifies what overrides are applied per directory.

  5. maxime1992 commented on Aug 20, 2020

    @maxime1992

    While this would be really awesome in some cases, I've started to use Betterer so that we can progressively improve our code base and avoid regression because a given flag is turned on locally to do some fixes and then off before pushing.

    Here's an example: https://dev.to/phenomnominal/stricter-typescript-compilation-with-betterer-dp7

  6. JohnForster commented on Feb 4, 2021

    @JohnForster

    Equally, it would be good to be able to turn it on globally, and then disable for specific files.

  7. y0nd0 commented on Feb 4, 2021

    @y0nd0

    In our case, we have a huge app that was written years ago with no strict mode.
    Today I want to use strict mode for cleaner code and fewer runtime errors.
    But we can't activate it, otherwise the code would shine like a Christmas tree.
    It is not very economical to convert all the code at once. It's too complex.
    So strict mode per file would be great. ...

  8. Lakston commented on Feb 9, 2021

    @Lakston

    I agree, there is no way we can migrate to strict mode in one swoop, we need to be able to do that on a per file basis.

  9. mrhut10 commented on Feb 19, 2021

    @mrhut10

    I would love this, or at mainly a way to turn off strict for a particular file.

    that way I can inforce team not to ever turn off scrict for whole project, just if something must get rushed to production they may use to send in a Merge request with stricted turned off for the one file they are rushing.

  10. TheNickmaster21 commented on Feb 22, 2021

    @TheNickmaster21

    This feature would be great. We recently converted a medium sized project to strict mode and it was a huge endeavor. We are glad we made the change but there is no way we could do the same thing on our larger projects any time soon. We would love to have this as a mechanism to use strict mode on new development. This would also be great when slowly converting a project.

  11. netpoetica commented on Feb 26, 2021

    @netpoetica

    I have a use case that may help justify this. I am using gRPC to generate .ts and .d.ts, but then I am using tsc to compile everything into a dist. The generated files must have strict = false in tsconfig, but the files I write I want to to have strict = true in tsconfig. It would be ideal if there was a way to have a tsconfig.override.json inside of the dir that holds those protoc-generated files that tells the global compile process "hey, these files should use these other rules"

  12. kamkry commented on May 26, 2021

    @kamkry

    While there are no built-in features like this in Typescript, we've created typescript-strict-plugin which allows you to turn on strict-mode in specific files or routes.
    Hope it helps!

  13. Bernold1 commented on Jun 30, 2021

    @Bernold1

    Is there any update on disabling strict mode for specific files?

  14. 13 remaining items

  15. jakebailey commented on Jul 17, 2022

    @jakebailey
    Member

    See also #31035, which has a WIP PR: #49886

    The PR's demo is exactly what this issue is asking for, I think. Wesley Wigham (@weswigham)

    (Maybe this is a dupe? Or the other way around?)

  16. AmirSavand commented on Aug 4, 2022

    @AmirSavand

    Oh, I need this so bad 😢.

  17. devanfarrell commented on Aug 22, 2022

    @devanfarrell

    I haven't tried it but I saw this TS plug-in shared. https://github.com/allegro/typescript-strict-plugin

  18. kepek commented on Feb 8, 2023

    @kepek

    👍

  19. ovcharik commented on Feb 6, 2025

    @ovcharik

    To solve the problem of migrating to strict type checks, I created @selectel/ts-check.

    In our project, we use separate configurations for building and type checking in IDE with strict mode. The utility is used in the CI/CD pipeline to check types of changed files in strict mode, like in the IDE.

  20. Gibbo3771 commented on Nov 20, 2025

    @Gibbo3771

    I haven't tried it but I saw this TS plug-in shared. https://github.com/allegro/typescript-strict-plugin

    Thought I would leave a comment here for anyone trying this in 2025. Plugin does not seem to work. It does not recognise the preprocess comment so enabling strict mode just enables it across the entire codebase.

  21. rwalle commented on Mar 5, 2026

    @rwalle

    I think this will be less relevant with TypeScript 6.0, when strict is true by default:

    https://devblogs.microsoft.com/typescript/announcing-typescript-6-0-beta/#simple-default-changes

    • For a JavaScript project that is already type checked or for TypeScript projects, this only will be relevant if you intentionally set strict to false and then still want to enable strict check on a file basis
    • For a JavaScript project that does not enable codebase-level type checking, // @ts-check will enable strict checking, and you do not need any additional flag to enforce strictness
  22. codethief commented on Mar 5, 2026

    @codethief

    I think this will be less relevant with TypeScript 6.0, when strict is true by default:

    Well, yeah, if you have already have strict type-checking enabled, you're obviously golden. That was already the case when the present ticket got filed almost 7½ years years ago. But the whole point of the ticket is that for existing projects

    [enabling] strict mode for the entire project is a daunting task that presents a barrier to adoption

    So that's why one needs to be able to do this incrementally, on a file-by-file basis.

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

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions