I have a fairly large Typescript project and I'd like to make it more strict. In particular, we have an issue with null-checking in a lot of places so I'd like to use the strict null-checks option. However, there are thousands of errors and its not exactly possible to fix that anytime soon.
But as I'm writing new code, I'd like to be strict with non-nullable types so I'm not piling onto the mess. Is there any way to incrementally improve the strictness of a Typescript project so all new code has strict null checks?
If you just start a new project I highly recommend using strict mode in TypeScript. It will help you to avoid errors, typos, and mistakes in your code in the future. Strict mode constricts you in ways of writing your code. On other hand, you won't choose the way that will bring you to make a mistake.
By default, TypeScript doesn't enable the strict mode: { "compilerOptions": { "target": "es5", ... }, ... } Also setting the value to false will fail: { "compilerOptions": { "strict": false, ... }, ... }
Disable strict checks entirely by setting strictTemplates: false in the application's TypeScript configuration file, tsconfig.
You can use typescript-strict-plugin which allows you to turn on strict mode in specific files or directories.
npm i -D typescript-strict-plugin
{
"compilerOptions": {
//...
"strict": false,
"plugins": [
{
"name": "typescript-strict-plugin",
}
]
}
}
Before:
//no strict comment here
...
const name: string = null; // no error here
After:
//@ts-strict
...
const name: string = null; // TS2322: Type 'null' is not assignable to type 'string'.
I think that the optimal solution is to extend the tsconfig.json with a new one which enables strictNullChecks and which will be used in the IDE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With