Sub topic 14
When to Use tsup or a Declaration Bundler Instead of Plain `tsc`
Assume you know TypeScript syntax, npm, tsconfig.json, and basic ESM imports and exports. This chapter focuses on build choices for a library that already has a public entry point and needs both JavaScript output and usable declaration files.
What Plain `tsc` Gives You
The TypeScript compiler, tsc, can compile each source file separately. With declaration: true, it also emits a .d.ts file for each source module. This approach is predictable and preserves the source module structure.
{
"compilerOptions": {
"target": "ES2020",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"rootDir": "src",
"outDir": "dist",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"strict": true
},
"include": ["src"]
}For a small library, this is often the best choice. It uses the same compiler that checks your code, produces standard output, and requires little configuration.
The output may contain several internal files:
src/
index.ts
slugify.ts
internal/
normalize.ts
dist/
index.js
index.d.ts
slugify.js
slugify.d.ts
internal/
normalize.js
normalize.d.tsYour exports map can expose only dist/index.js, even though the other files exist in the package.
What a Bundler Adds
A JavaScript bundler combines modules into one or a few files. Tools such as tsup use esbuild to produce formats such as ESM and CommonJS, often with minification, source maps, and external dependency handling.
A typical tsup configuration looks like this:
// tsup.config.ts
import { defineConfig } from "tsup";
export default defineConfig({
entry: ["src/index.ts"],
format: ["esm", "cjs"],
dts: true,
sourcemap: true,
clean: true,
splitting: false,
outDir: "dist",
external: ["react"]
});The dts: true option asks the tool to generate declarations for the public entry point. The exact declaration behavior depends on the tool version and its declaration-bundling implementation, so inspect the generated files rather than assuming they are correct.
Bundling is useful when you want a small, convenient distribution:
- one public JavaScript file per format
- fewer internal files to publish
- easier browser-oriented builds
- a single declaration entry point
- automatic handling of several output formats
A bundler also introduces another build layer. It can hide modules that should remain separate, mishandle unusual TypeScript patterns, or produce declarations that expose private paths.
Declaration Bundling and Its Trade-offs
A declaration bundler combines many .d.ts files into a smaller public type surface. rollup-plugin-dts and Microsoft API Extractor are common choices. API Extractor can also report which exported types are intended to be public and detect forgotten release notes or incompatible API changes.
The key distinction is:
| Approach | JavaScript output | Declaration output | Best fit |
|---|---|---|---|
| Plain `tsc` | One file per source module | One file per source module | Small, transparent libraries |
| `tsup` | Bundled ESM and/or CommonJS | Usually bundled declarations | Simple multi-format packages |
| API Extractor | Does not normally bundle JavaScript | Rolled-up, reviewed API | Large libraries with strict API governance |
A declaration bundle is not automatically better. If a public function returns a type imported from a dependency, that dependency still needs to be available through dependencies, peerDependencies, or a suitable @types package. Bundling declarations can also duplicate types or create name collisions.
Choosing a Build Strategy
Use plain tsc when:
- your output can mirror the source directory
- you publish one module format
- transparent module boundaries matter
- you want the fewest moving parts
Use tsup when:
- you need ESM and CommonJS output
- you want a concise configuration
- bundling reduces unwanted internal files
- your library does not rely on difficult runtime transformations
Use API Extractor or another declaration bundler when:
- the public API must have one reviewed
.d.tsfile - you need API reports or compatibility checks
- many internal types should remain hidden
- your project has a formal release process
Keep tsc --noEmit or an equivalent type-checking step even when a bundler performs a build. A bundler may transpile code without performing every type check that tsc performs.
Exercise
A library has one ESM entry point, five small source modules, and no need to hide internal files. Which build strategy is the simplest starting point, and why?
Show answer
Use plain tsc with declaration output. It preserves the module structure and avoids adding a bundler before the library needs one.