TypeScript Strict Mode Plugin
The `typescript-strict-plugin` enables gradual adoption of TypeScript's strict mode in existing large projects. Instead of refactoring an entire codebase at once, developers can configure this plugin to apply strict type checking to specific files or directories, or enable strict mode by default and opt out problematic files. The current stable version is 2.4.4. It offers a clear migration path with its `update-strict-comments` script, which automatically adds `//@ts-strict-ignore` comments to files containing strict errors. This allows new or refactored code to benefit from strict checks immediately, while legacy code can be addressed incrementally. It also provides a CLI tool, `tsc-strict`, to ensure compile-time strictness checks, as TypeScript language service plugins typically only affect IDE feedback. The project appears to have an active release cadence, with recent updates addressing bug fixes and compatibility. Its key differentiator is providing a flexible, file-level control over strict mode, effectively solving the "all-or-nothing" barrier for large-scale migrations.
Common errors
-
Error: Plugin 'typescript-strict-plugin' not found. Make sure it's installed and listed in your tsconfig.json.
cause The plugin is not installed as a `devDependency` or its name is misspelled in `tsconfig.json`.fixRun `npm install --save-dev typescript-strict-plugin` and verify that `"name": "typescript-strict-plugin"` is correctly configured in your `tsconfig.json` under `compilerOptions.plugins`. -
tsc-strict: command not found
cause The `tsc-strict` executable is not in your system's PATH when you try to run it directly.fixExecute `tsc-strict` using `npx tsc-strict` or by defining a script in your `package.json` (e.g., `"scripts": { "strict-check": "tsc-strict" }`) and running `npm run strict-check`. -
Strict errors are not appearing in my terminal when I run `tsc`.
cause The plugin's primary function is for IDE feedback. The default `tsc` command does not run the plugin's strictness checks.fixYou need to integrate the `tsc-strict` CLI tool into your build process. Add it to a `package.json` script, for example: `"scripts": { "build": "tsc --noEmit && tsc-strict" }`. -
My files are still showing strict errors even with `//@ts-strict-ignore` at the top.
cause The `//@ts-strict-ignore` comment might not be at the absolute top of the file, or your `tsconfig.json` has `"strict": true`, overriding the plugin.fixEnsure `//@ts-strict-ignore` is the very first line of your file. Also, confirm that `"strict": false` is set in your `compilerOptions` within `tsconfig.json` for the plugin to take effect.
Warnings
- breaking Version 2.0 introduced a breaking change by inverting the default strictness behavior. Previously, files were non-strict by default and opted-in with `//@ts-strict`. Now, files are strict by default and opt-out using `//@ts-strict-ignore`.
- gotcha TypeScript Language Service Plugins (like this one) primarily affect IDE feedback. To enforce strict checks at compile-time during your build process, you *must* use the `tsc-strict` CLI utility provided by the package.
- gotcha For the plugin to manage partial strictness (i.e., making some files strict while others are not), your `tsconfig.json`'s `compilerOptions.strict` flag must be set to `false`. If `strict` is `true`, the plugin's effect will be overridden by the global compiler setting.
- gotcha Command-line arguments passed to `tsc-strict` (e.g., `--strictNullChecks false`) will override the compiler options configured in your `tsconfig.json` for the strict checks performed by `tsc-strict`. This can lead to unexpected behavior if not understood.
- gotcha The `//@ts-strict-ignore` comment must be placed at the very top of a TypeScript file to be effective in disabling strict checks for that file. If it's not the first line (after any shebang or license comments), it may not be parsed correctly by the plugin.
Install
-
npm install typescript-strict-plugin -
yarn add typescript-strict-plugin -
pnpm add typescript-strict-plugin
Imports
- typescript-strict-plugin (in tsconfig.json)
{ "plugin": "typescript-strict-plugin" }{ "name": "typescript-strict-plugin" } - update-strict-comments
node update-strict-comments
npx update-strict-comments
- tsc-strict
tsc-strict
npm run typecheck
Quickstart
{
"name": "my-strict-project",
"version": "1.0.0",
"devDependencies": {
"typescript": "^5.0.0",
"typescript-strict-plugin": "^2.0.0"
},
"scripts": {
"typecheck": "tsc --noEmit && tsc-strict"
}
}
// tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"strict": false, // Crucial: Set to false for the plugin to manage strictness
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"plugins": [
{
"name": "typescript-strict-plugin",
"paths": ["./src"], // Optionally specify paths to make strict by default
"excludePattern": ["**/*.spec.ts"]
}
]
},
"include": ["src/**/*.ts"]
}
// 1. Install dependencies
// npm install --save-dev typescript typescript-strict-plugin
// 2. Run the migration script to ignore files with existing errors
// npx update-strict-comments
// 3. To run compile-time strict checks
// npm run typecheck