Sub topic 3
Dependencies in Public Types and Releasing a TypeScript Library
Assume you know TypeScript types, npm, package.json, and basic tsc usage. This chapter focuses on dependencies that appear in your generated .d.ts files and on releasing the package safely.
Dependencies, peer dependencies, and `@types`
A public type is any type that appears in an exported function, class, or interface. If that type comes from another package, consumers may need that package to compile their code.
Use dependencies when your library needs the package at runtime or when its declarations require the package's types:
{
"dependencies": {
"date-fns": "^4.0.0"
},
"devDependencies": {
"typescript": "^5.0.0"
}
}Use peerDependencies when the application supplies the package, often because only one compatible copy should exist. UI libraries commonly declare a framework such as React as a peer dependency. Add a matching @types/... package when the public declarations refer to types that it provides.
Keep a type package in devDependencies only when no emitted public declaration refers to it. Otherwise, a consumer may receive a declaration that cannot be resolved.
The relationship is:
Published files and release checks
Limit the package with files so source, tests, and local configuration do not get published accidentally.
{
"files": ["dist"],
"publishConfig": { "access": "public" }
}Run npm pack --dry-run to inspect the archive before publishing. Then check metadata and type resolution:
npm run build
npm pack --dry-run
npx publint
npx @arethetypeswrong/cli --pack .Install the resulting tarball in a separate test project and compile a small consumer. This catches missing declarations, broken exports, and unresolved type dependencies.
Versioning and publishing
After checks pass, choose a semantic version: increment the patch for fixes, minor for compatible features, or major for breaking changes.
npm version patch
npm publishFor a scoped public package, set "publishConfig": { "access": "public" }. Confirm the package name, version, registry, and included files before publishing.
When plain `tsc` is not enough
Plain tsc is a good choice when you want transparent output and one declaration file per source module. Use tsup when you also need bundled JavaScript, multiple formats, minification, or a simpler build command. Use a declaration bundler when consumers should see one flattened .d.ts entry point. Bundling can hide module boundaries and may require extra configuration for complex type dependencies.
Exercise
When should a public @types package be a production dependency rather than only a development dependency?
Show answer
When emitted public declarations refer to types from that package and consumers need it to resolve those declarations.