Transitioning an existing project to TypeScript seems tough. In this post, I will show a 3 step plan to do so without disruption or undue stress.
A couple of warnings before we get started. First, improve low test coverage to ensure project stability during the transition. Second, refrain from functional changes to verify the project isn’t working because of TypeScript and not a recently introduced bug. With the warnings out of the way, let’s jump in!
From the “TypeScript 3 Fundamentals” course on Frontend Masters, the instructor Mike North laid out a 3 step plan as follows:
- Rename file extensions from
.js
to.ts
and fix type errors - Declare explicit
any
types by setting“noImplicitAny”: true
in thetsconfig.json
and narrow down the types where TypeScript complains. - Slowly enable strict mode by setting each of the following options in the
tsconfig.json
:”strictNullChecks”: true
,”strict”: true
,”strictFunctionTypes”: true
,”strictBindCallApply”: true
and fixing each error as necessary.
After completing each step with passing tests, submit a pull request to delineate each step and stay on track. And that’s it!
By ratcheting up type strictness, the transition from JavaScript to TypeScript isn’t so hard after all.