Sub topic 10
Add Types for Third-Party Packages
Assume you know how to install npm packages, write imports, and run the TypeScript compiler. This chapter focuses on libraries that your JavaScript code already uses.
TypeScript needs type information for every imported package. A package can provide that information in three common ways:
| Package situation | What you do |
|---|---|
| The package includes types | Install the package and import it normally |
| The package has a separate declaration package | Install its matching `@types` package |
| No declarations exist | Write a small declaration or use a temporary fallback |
A declaration file usually ends in .d.ts. It describes the package's functions, objects, and return values without containing the implementation.
Find the package's type information
First, check whether the package documentation says that it includes TypeScript support. If it does, no additional package is needed. If it does not, search npm for a matching package under the @types scope.
For example, install Lodash and its community-maintained declarations:
npm install lodash
npm install --save-dev @types/lodashThe package name and declaration name usually match. lodash uses @types/lodash, while a package named some-library would commonly use @types/some-library.
Use the declarations in converted code
Suppose the JavaScript file contains this import:
import _ from "lodash";
const names = ["ada", "grace", "linus"];
const result = _.map(names, (name) => name.toUpperCase());
console.log(result);With @types/lodash installed, TypeScript knows that names is an array of strings. It can check the callback parameter and infer that result is a string[].
Without declarations, the imported function may become any. The compiler then accepts incorrect calls because it has no reliable description of the library.
If your project uses a CommonJS-style package and the import produces an error, check the module settings in tsconfig.json. A configuration using "esModuleInterop": true often permits default imports for packages that expose CommonJS exports. Otherwise, use the package's documented import form, such as:
import * as path from "node:path";Do not install @types automatically. Some packages ship their own declarations, and installing an outdated separate package can create conflicts.
When no declaration package exists
For a small untyped package, create a local declaration file such as src/vendor.d.ts:
declare module "legacy-widget" {
export function start(options: { debug?: boolean }): void;
}This tells the compiler only what your code relies on. Keep the declaration narrow, and improve it when you learn more about the package. Avoid declaring the whole module as any, because that removes checking at every call site.
Exercise
You import date-fns, but TypeScript reports that it cannot find a declaration file. What should you check first?
Show answer
Check whether the package includes its own types. If it does not, install the matching @types/date-fns package if one exists. Do not add a broad any declaration before checking those options.