strict per file
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.
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).
//@ts-strict
My suggestion meets these guidelines:
Would love to see this feature implemented as well. We have quite sizeable project and going "all strict" is not an option right now.
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.
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.
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.
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
Most helpful comment
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.