Sub topic 14
Publishing a Typed Package with `types` and `exports`
Prerequisites and the package contract
This chapter assumes that you know basic TypeScript types, can run tsc, and understand JavaScript import and export statements. It also assumes basic npm usage.
A typed package provides two related outputs:
- JavaScript files that run in the consumer's environment.
- Declaration files, such as
.d.ts, that describe the JavaScript to TypeScript.
The package contract tells consumers where to find both outputs. A package should expose only its supported public entry points. The exports field controls those entry points, while the types field points TypeScript to the declarations.
A small package might have this structure:
my-lib/
├── package.json
├── src/
│ └── index.ts
└── dist/
├── index.js
└── index.d.tsThe source module can export normal TypeScript code:
// src/index.ts
export function add(a: number, b: number): number {
return a + b;
}Build this source with declaration output enabled:
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"strict": true,
"declaration": true,
"outDir": "dist"
},
"include": ["src"]
}The compiler emits JavaScript and a declaration file:
// dist/index.d.ts
export declare function add(a: number, b: number): number;The declaration contains the public type information, but not the function body.
Configure `package.json`
For an ES module package, use "type": "module". Then map the package's root entry point with exports:
{
"name": "my-lib",
"version": "1.0.0",
"type": "module",
"files": ["dist"],
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
}The "." key represents the package root. A consumer can write:
import { add } from "my-lib";
console.log(add(2, 3));TypeScript checks the import against ./dist/index.d.ts. Node.js loads ./dist/index.js.
Put the "types" condition before runtime conditions. TypeScript's package resolution checks conditional exports and selects the declaration path for type checking. The "default" condition supplies the runtime file.
The older top-level "types" field is also useful:
{
"types": "./dist/index.d.ts"
}Use it when the package has one simple entry point and does not need conditional exports. When exports is present, place the declaration path inside the matching export condition so the type and runtime entry points stay aligned.
| Package field | Main purpose |
|---|---|
| `type` | Selects how Node.js interprets `.js` files |
| `main` | Older runtime entry-point field |
| `types` | Points TypeScript to a declaration entry point |
| `exports` | Restricts and maps public package entry points |
| `files` | Limits which files are included when publishing |
Multiple entry points and common failures
A package with a subpath can map each public entry explicitly:
{
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./format": {
"types": "./dist/format.d.ts",
"default": "./dist/format.js"
}
}
}This lets consumers import my-lib/format, but prevents accidental imports from private source files.
A common failure occurs when JavaScript is published without its matching .d.ts files. The code runs, but consumers lose type checking. Another occurs when exports points to dist/index.js while types points to a declaration for a different API. Keep both files generated from the same source.
Exercise
In an exports entry, what should the types condition point to, and what should the default condition point to?
Show answer
The types condition should point to the matching .d.ts file, and the default condition should point to the JavaScript file that runs in the target environment.