How to Install Types for Packages

When you develop an app you often use some third-party packages as dependencies. Not all of them are written in TypeScript, but many provide type declarations for a developer to be able to include those in a project.

The biggest repository with type declarations is DefinitelyTyped. It contains type declarations for packages like React, Redux, Mobx, and many others.

When You Need to Install Types#

Some packages already have .d.ts files in them. These files contain type declarations. If your dependency package has it, you won't need to install types manually.

If a package doesn't have .d.ts file and is written in plain JavaScript, you might need to find type declarations yourself.

Let's say you want to use react-transition-group package. You installed the package itself and try to import it in a React + TS project:

IDE will show an error that the project can't be compiled:

How to Install Types#

A package may recommend installing types from a DefinitelyTyped or another repository in its documentation.

If so, run all the commands recommended in docs.

Otherwise, try to install types from DefinitelyTyped manually. In the case of react-transition-group:

Usually, type declaration name consists of the @types/ prefix and the package name after it. (In some rare cases you may want to google the name first.)

When types are installed IDE should hide the error and compile the project.