Converting a project to TypeScript 2.0

Oct 22, 2016

If there is one thing the TypeScript people should improve, it's documentation. I just went through a small Odyssey trying to convert a project to TypeScript 2.0 and the new way of getting definition files. Hint: Look at how the Rust project does documentation.

Updating TypeScript

Go into your package.json, set TypeScript to version 2.something and run npm install. Done. Try to compile your project, it probably won't because the compiler is a bit more strict. The error messages, however, should give you an idea what needs to be changed, go and fix your code. Visual Studio Code uses the globally installed TypeScript version, update that one as well by running npm install -g - yes, not update, don't ask why. I make this mistake every. single. time.

Use @types

You can now install type definitions via npm, typings/tsd are no longer needed. Installation goes like this:

npm install --save @types/react

All DefinitelyTyped definitions are available, so you might as well do this now. After installing all typings, remove the reference path to the old definitions, try to build and observe how TypeScript cannot resolve a single module. First, we have to tell TypeScript that we're using node modules:

// tsconfig.json
{
  "compilerOptions": {
    "moduleResolution": "node"
  }
}

Now, you might actually be able to compile. Unless you're using any global non-standard functions like require or core-js shims. Remember that you had to explicitly load typings using the reference path before? This is no longer necessary, but this also means TypeScript has no idea what typings are available. When you import something, they are loaded automatically, but if something should always be loaded, you need to configure that:

// tsconfig.json
{
  "compilerOptions": {
    "types": [
      "node", "mocha", "core-js"
    ]
  }
}

Done, now your project should work as usual and with one less tool required.

Back to blog