Sub topic 10
Dependencies, peer dependencies, and `@types` packages in public types
Assume you know TypeScript imports and exports, tsconfig.json, and basic npm package management. This chapter focuses on one question: what must your published package declare when its exported types refer to other packages?
Why public types create dependencies
A declaration file describes your library's public API. If that description imports a type from another package, TypeScript must be able to resolve that package when a consumer checks your library.
// src/client.ts
import type { AxiosInstance } from "axios";
export interface Client {
http: AxiosInstance;
getUser(id: string): Promise<unknown>;
}The generated dist/client.d.ts will still refer to axios. The import was removed from the JavaScript because it used import type, but the declaration still needs the axios package and its type declarations.
A package used by your public declarations is a type dependency. It is different from a package used only in your source implementation.
| Dependency kind | Used by runtime code | Used by public types | Typical package.json location |
|---|---|---|---|
| Runtime dependency | Yes | Maybe | `dependencies` |
| Host-provided dependency | Maybe | Maybe | `peerDependencies` |
| Internal build dependency | No | No | `devDependencies` |
| Public `@types` package | No | Yes | `dependencies` or `peerDependencies` |
Choosing the dependency section
Put a package in dependencies when your published JavaScript needs it or when consumers need it to resolve your public declarations.
For example, if your declarations refer to decimal.js, consumers should receive it when they install your package:
{
"name": "@acme/pricing",
"version": "1.0.0",
"dependencies": {
"decimal.js": "^10.4.3"
},
"devDependencies": {
"typescript": "^5.7.0"
}
}A peer dependency is a package that the consuming application must install and control. Use one when your library integrates with a host ecosystem and should share the host's copy. React components commonly use this arrangement.
{
"peerDependencies": {
"react": "^18.2.0 || ^19.0.0"
},
"peerDependenciesMeta": {
"react": {
"optional": false
}
},
"devDependencies": {
"@types/react": "^18.3.0",
"typescript": "^5.7.0"
}
}The peer package itself can provide the public types. If the peer package does not bundle types, declare the matching @types package as a peer dependency too, or arrange for your declarations not to expose that package's types.
`@types` packages and public API design
An @types package contains declaration files for a JavaScript package that does not ship its own types. If your public API exposes a type from that package, consumers need access to the declaration package.
// src/middleware.ts
import type { Request, Response } from "express";
export type Middleware = (
request: Request,
response: Response
) => void | Promise<void>;Here, express is part of the public type surface. A consumer compiling code that uses Middleware must resolve both express and its declarations from @types/express.
Prefer to expose your own types at the boundary when you do not want consumers tied to a framework:
export interface RequestLike {
method: string;
path: string;
}
export type Middleware = (request: RequestLike) => void | Promise<void>;This reduces coupling, but it may omit useful framework-specific behavior. Choose based on whether consumers should depend on the external type as part of your supported API.
Check generated declarations
Build before publishing and inspect the declaration imports:
npm run build
grep -R 'from "' dist --include='*.d.ts'Every package named by those imports must be available to a consumer through dependencies, peerDependencies, or a deliberately documented installation requirement.
Exercise
A library imports type { Widget } from widget-core, and the generated .d.ts still contains that import. The library lists widget-core only in devDependencies. What should change?
Show answer
Move widget-core to dependencies or peerDependencies, depending on whether the library supplies the package or the consuming application must supply it.