Happy Hacking

moon indicating dark mode
sun indicating light mode

Moving to strict TypeScript

September 27, 2019

At my current company I'm working on project that has passed through many hands. A lot of good ideas and various frameworks have found their way into this project. After the project was handed over to us, we decided to keep the code base but strike a new path towards React and TypeScript.

Unfortunately though, TypeScript was not configured to be strict. Meaning no strict null checks and implicit any's are all over the place. I don't think I have to explain to you how helpful it is to have those compiler flags enabled 💁‍♀️

During the past weeks we had the opportunity to address various tech debts in the sprint. So we decided to enabled the "strict": true compiler flag and fix all warnings.

One does not simply migrate to TypeScript with strict mode enabled

With more than 100k lines of code just compiling it with "strictNullChecks": true produced some 1200 errors 😲. Not to mention the errors reported with the noImplicitAny flag enabled. Branching of the code to fix those ~3000 errors would be a huge amount of work. But the most important issue here is, that you can't tell with confidence, that the changes you would make, would work properly.

We wanted something that would incrementally bring the benefits of TypeScript's strict mode to all engineers on the team. A way that allows us to break the work into smaller changes, making the code a little bit safer overtime. Turns out that TypeScript does not have the right tools for the job. That's why we wrote ts-strictify.

How does it work?

When you start working on a new feature or fixing a bug, you will modify the code base in one way or another. ts-strictify will take a look at these changes - and only these changes (!) and will complain, if the files you have touched are not strict compliant.

Thats is different than TypeScript works. You could check a single file against the compiler, but the compiler would also look up the imports and the imports of the imports. Not exactly what you want, when you are looking for incrementally update path.

Conclusion

At the moment we are still gathering further experience and gradually adapting ts-strictify. But the start has been made and we are looking forward having a strict TypeScript code base in the next weeks.

That's it. Happy Hacking. ✌️