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.
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
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.
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
- Saves library as dependency in
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.
Please visit http://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html to learn more about the type declaration files.