Migrating from JS to TS

TS docs

Step 1

Compiling in loose mode

"allowJs": true, /* Allow javascript files to be compiled. */
"checkJs": true, /* Report errors in .js files. */
  • Convert all js files to ts files

  • Allowing implicit any

    "noImplicitAny": false,
    /* true = Raise error on expressions
    and declarations with an implied 'any' type. */
  • Fix things which will give intellisense, but won't break or give any compiler errors. Like some class property types ..etc

  • Run unit tests if any

Step 2

Explicit any

  • Change setting implicit any

    "noImplicitAny": true,
    /* Raise error on expressions and declarations
    with an implied 'any' type. */
  • Where ever possible provide specific type or explicit any

  • Can import types of 3rd party libs from DefinitelyTyped //like jquery, node, koa, mongoDB types

  • Run unit tests if any

Step 3

Squash explicit any & enable strict mode

  • Do below incrementally in small steps (enable - fix few files - disable & commit)

    "strict": true,
    "strictNullChecks": true,
    /* Enable strict null checks like CreatedOn:Date | null ..etc*/
    "strictFunctionTypes": true,
    /* Enable strict checking of function types. */
    "strictBindCallApply": true,
    /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
  • Replace explicit any with more appropriate types

  • Avoid unsafe casts like 'as' keyword (someNumber as string)