Tackling TypeScript
Please support this book: buy it or donate
(Ad, please don’t block.)

10 Strategies for migrating to TypeScript

This chapter gives an overview of strategies for migrating code bases from JavaScript to TypeScript. It also mentions material for further reading.

10.1 Three strategies

These are three strategies for migrating to TypeScript:

More information:

10.2 Strategy: mixed JavaScript/TypeScript code bases

The TypeScript compiler supports a mix of JavaScript and TypeScript files if we use the compiler option --allowJs:

At first, there are only JavaScript files. Then, one by one, we switch files to TypeScript. While we do so, our code base keeps being compiled.

This is what tsconfig.json looks like:

  "compilerOptions": {
    "allowJs": true

More information:

10.3 Strategy: adding type information to plain JavaScript files

This approach works as follows:

This is how we specify static types for plain JavaScript via JSDoc comments:

 * @param {number} x - The first operand
 * @param {number} y - The second operand
 * @returns {number} The sum of both operands
function add(x, y) {
  return x + y;
/** @typedef {{ prop1: string, prop2: string, prop3?: number }} SpecialType */
/** @typedef {(data: string, index?: number) => boolean} Predicate */

More information:

10.4 Strategy: migrating large projects by snapshot testing the TypeScript errors

In large JavaScript projects, switching to TypeScript may produce too many errors – no matter which approach we choose. Then snapshot-testing the TypeScript errors may be an option:

More information:

10.5 Conclusion

We have taken a quick look at strategies for migrating to TypeScript. Two more tips: