Sub topic 11
Using bundled types and @types packages
Assume you know basic TypeScript types, can run tsc, and understand npm packages and imports. This chapter explains how TypeScript gets type information for JavaScript packages.
Where package types come from
A JavaScript package can provide types in two ways:
- Bundled types: The package includes its own
.d.tsfiles. @typespackage: The types are published separately in the DefinitelyTyped collection.
TypeScript checks a package's metadata and files when it resolves an import. A package commonly points to its declarations with the types or typings field in package.json.
{
"name": "example-library",
"main": "./dist/index.js",
"types": "./dist/index.d.ts"
}If a package has bundled types, install only the package. For a package without types, install the matching @types package as a development dependency.
| Runtime package | Separate type package |
|---|---|
| `lodash` | `@types/lodash` |
| `express` | `@types/express` |
| `date-fns` | Usually none; types are bundled |
Installing and using `@types`
The name of a DefinitelyTyped package usually adds the @types/ scope and keeps the original package name:
npm install lodash
npm install --save-dev @types/lodashThe type package does not add runtime code. The application still imports lodash; TypeScript finds declarations from @types/lodash while checking the source.
import map from "lodash/map";
const names = map(
[{ name: "Ada" }, { name: "Grace" }],
(person) => person.name
);
console.log(names);When the installed versions disagree, declarations can describe APIs that your runtime does not actually provide. Keep the package and its type package compatible, especially after upgrades.
How TypeScript discovers declarations
For an import such as import express from "express", TypeScript searches according to the active module-resolution strategy. It examines the package's metadata, declaration files, and recognized node_modules/@types directories.
The types compiler option can restrict which ambient packages are included globally:
{
"compilerOptions": {
"strict": true,
"module": "nodenext",
"moduleResolution": "nodenext",
"types": ["node"]
}
}This setting does not block declarations that an imported package supplies directly. It mainly controls automatically included global packages, such as Node.js globals and test-runner globals. If a library's types seem missing, first check that the package is installed, its version supports TypeScript, and your import path matches its published declarations.
Common errors and fixes
- “Could not find a declaration file”: install the package's documented
@typespackage, or write a local ambient declaration when no package exists. - The
@typespackage is not found: check its name and whethernode_modulesis in the expected project directory. - The types are too old or too new: select a compatible version, or upgrade both packages together.
- Types work in one project but not another: compare
tsconfig.json, especiallymoduleResolution,typeRoots, andtypes. - A subpath import fails: the package may not publish declarations for that subpath, or its
exportsconditions may omit the type entry.
Exercise
You install some-library, but TypeScript reports that it has an implicit any type. What should you check first?
Show answer
Check the library documentation for a bundled declaration file or a matching @types/some-library package. Install the type package as a development dependency, then verify that the installed versions and import path match.