Advertisement
Advanced TypeScript, Declaration Files

How does TypeScript resolve which .d.ts file to use for a given package, and what does the 'types'/'typings' field in package.json control?

Short answer

TypeScript checks a package's package.json for a 'types' (or legacy 'typings') field pointing to its bundled declaration file; if absent, it falls back to looking for an @types/ package in node_modules, and finally to an implicit 'any' type if neither exists (unless…

Save TypeScript mock interview
Advertisement
AModel answer
TypeScript checks a package's package.json for a 'types' (or legacy 'typings') field pointing to its bundled declaration file; if absent, it falls back to looking for an @types/<package-name> package in node_modules, and finally to an implicit 'any' type if neither exists (unless noImplicitAny blocks that).
// package.json of a library
{
  "name": "my-lib",
  "main": "dist/index.js",
  "types": "dist/index.d.ts"
}
Real-world example
Publishing your own npm package with bundled types so consumers get type checking without a separate @types install.
Likely follow-ups
  • What happens if a package has NEITHER a 'types' field NOR a matching @types package available?
Related topics