How TypeScript Finds Your Package's Types, Illustrated
If your JavaScript package is used from TypeScript, its declaration files are part of your public API, whether you wrote the package in TypeScript or not. Most "types are broken" bug reports come down to the compiler looking in a different place from where you put the files. The figures below, described as captions with walkthroughs, follow the compiler's search for a hypothetical package called Package M.
Figure 1: Two places types can live
Caption: two columns. On the left, a box labelled "package-m" containing index.js and index.d.ts. On the right, the same package without a .d.ts, plus a separate box labelled "@types/package-m" with a dotted line to it.
- Bundled types ship inside your package and always match the published version.
- Community types live in the DefinitelyTyped repository and are published under the
@typesscope. They are maintained separately and can lag behind. - If you can generate declarations, bundle them. When you start bundling, mention it in your changelog so users can drop the
@typespackage.
Figure 2: The lookup without an exports map
Caption: a flowchart starting at "import from 'package-m'". First diamond: "types (or typings) field?" Yes leads to "use that file". No leads to a second diamond: "main has a sibling .d.ts?" then to "look in @types".
Older module resolution settings follow roughly this path. The types field in package.json is the clearest signal; set it even if a sibling file would be found anyway.
Figure 3: The lookup with an exports map
Caption: the same import, now entering a box labelled "exports". Inside, conditions are stacked top to bottom: types, import, require, default. An arrow enters at the top and stops at the first match.
Under the node16, nodenext and bundler module resolution modes, TypeScript reads exports and ignores top-level fields for paths the map covers. Conditions are checked in the order written, which is why types must come first in each block.
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./errors": {
"types": "./dist/errors.d.ts",
"default": "./dist/errors.js"
}
}
Every subpath needs its own types entry. A forgotten one gives users an implicit any or a resolution error on that import only.
Figure 4: Declarations that match the format
Caption: a pair of files, index.js marked "ESM" next to index.d.ts, and index.cjs marked "CJS" next to index.d.cts. A red cross over a line linking the CJS file to the plain .d.ts.
TypeScript infers whether a declaration describes ESM or CommonJS from its extension and the nearest "type" field. Pointing CommonJS consumers at an ESM-flavoured declaration produces confusing errors about default imports. If you ship both formats, ship declarations for both. The dual ESM/CJS tutorial shows a complete layout.
Figure 5: Testing the declarations
Caption: a small consumer project folder labelled "types-smoke" containing a tsconfig.json and one check.ts file, with an arrow from the packed tarball into its node_modules.
- Install the packed tarball, not a symlink to your source folder.
- Run
tsc --noEmitwith eachmoduleResolutionmode you support. - Write a few type-level assertions for important signatures, using a tool such as tsd or the type-testing features of your test runner.
- Run a checker built for packages, such as Are the Types Wrong?, against the tarball.
Checklist before you publish
typesappears first in every exports condition block.- The declaration files are included in the
fileslist. - Declaration changes are reviewed like code changes; narrowing a parameter type can break callers.
- Your semver policy states whether type-only changes can land in minor releases.