1.10. Installing type declaration files

If you have been working with TypeScript for a while you are probably familiar with the DefinitelyTyped project on GitHub. The creators of this repository describe it as:

The repository for high quality TypeScript type definitions.

The DefinitelyTyped project is the best place to search for the type declaration files of third party JavaScript libraries.

In TypeScript 2.0 onwards, it has become much easier to consume declaration files and you no longer need type declaration files management tools like tsd or typings but npm to install and update type declaration files.

In the early days of TypeScript development, the language was mostly used by developers with a background on Microsoft technologies. This explains why many software engineers decided to install the third-party type declaration files using NuGet. NuGet is the package manager for the Microsoft development platform including .NET.

Getting ready

You do not require any type declaration files management tool (i.e. tsd or typings) any more in TypeScript 2.0 or higher to install type declaration files but already installed npm on your machine.

How to do it

You can find the declaration files for your favorite library at type search i.e. https://aka.ms/types

In many cases, you will find a type declaration file but don’t panic if there are no files available as we will learn how to overcome that problem later in this chapter.

If there is type declaration files available for the library, run the following command to install it:

$ npm install @types/<library_name> --save

How it works

Above npm install commands does two things.

  • Grabs the declarations files for the library and saves it to a directory @types <library name> within node_modules folder
  • Saves library as dependency in package.json

There’s more

You can also update or uninstall type declaration files for a particular library like any other npm packages also explained in earlier section "Working with npm packages" of this chapter.

See also

Please visit http://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html to learn more about the type declaration files.

Shiv Kushwaha