DEV Community

Cover image for Turn on TS flag evolutionarily
Przemyslaw Jan Beigert
Przemyslaw Jan Beigert

Posted on • Edited on

Turn on TS flag evolutionarily

Introduction

After migration a JavaScript project to TypeScript it is good to turn on some compilerOptions flags. Some of them are easy, like strictBindCallApply but for example strictNullChecks can throw many errors: Found 649 errors in 184 files. Migrating all of them in one pull request can create many bugs or merge conflicts. On the other hand without this migration the amount of strictNullChecks errors can increase.

Unfortunately there's no way to turn a compilerOptions flag on only part of the project. However we can track the amount of these errors.

Implementation

"scripts": {
  "typeCheck": "tsc",
  "typeCheck:checkNulls": "tsc --strictNullChecks true"
}
Enter fullscreen mode Exit fullscreen mode

Now we can check how many errors we have. Of course this script will always fails so we can not add it to the CI process. To add it we have to add some pipes:

yarn typeCheck:checkNulls | grep error | wc -l
Enter fullscreen mode Exit fullscreen mode

Grep will filter files without error and wc -l will reduce lines with errors to just a number. Then simple > operation will check if the amount of errors wasn't increased. In bash it looks like this:

if [ $(yarn typeCheck:checkNulls | grep error | wc -l) -gt 649 ]; then exit 1; fi
Enter fullscreen mode Exit fullscreen mode

Summary

This solution isn't perfect. For example you can fix old one then create a new error and CI will not catch it. Also IDE will not inform about any strictNullChecks error because in the tsconfig.json flag is set as false. I this case simplicity is the main advantage: one line in package.json and one in CI config. But compared to alternatives: migrating all by once or separate modules from the project it doesn't look so bad.

Top comments (0)