Publishing Types with Your Package
You'll learn how to configure your npm package so consumers automatically get TypeScript type information when they install it.
The Publishing Problem
When you publish a JavaScript package to npm, TypeScript users who install it won't automatically get type checking or autocomplete unless you include type information. You need to explicitly configure your package to ship types alongside your compiled code.
Think of it like publishing a product with its instruction manual — the code works without types, but the experience is far better when types are included right in the box.
Two Essential Configuration Steps
1. Generate Declaration Files
In your tsconfig.json, enable declaration file generation:
{
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"outDir": "./dist"
}
}
When you compile, TypeScript will now generate .d.ts files alongside your .js files in the dist folder. These declaration files contain all the exported type information.
2. Point package.json to Your Types
In your package.json, add the types (or typings) field:
{
"name": "my-package",
"version": "1.0.0",
"main": "./dist/index.js",
"types": "./dist/index.d.ts"
}
This tells TypeScript where to find your package's type definitions. When someone installs your package, their editor will automatically load these types.
The Files to Include
Make sure your published package includes both the compiled JavaScript and the declaration files:
{
"files": [
"dist/**/*.js",
"dist/**/*.d.ts"
]
}
Now when consumers install your package, they get full TypeScript support automatically — no need for separate @types packages.
Key Takeaway: Enable declaration: true in your tsconfig.json and add a types field in package.json pointing to your main .d.ts file to publish TypeScript-ready packages that provide instant autocomplete and type safety for all users.