Sub topic 1
Structuring a library: source, build output and public entry point
A publishable TypeScript library needs a clear boundary between its development files and its consumer-facing files. Keep source code in src/, write compiled output to dist/, and expose the supported API through one public entry point.
This structure makes the package easier to build, test, and maintain:
| Directory or file | Purpose |
|---|---|
| `src/` | TypeScript source that you edit |
| `src/index.ts` | Public entry point that chooses what consumers can import |
| `dist/` | Generated JavaScript and declaration files |
| `package.json` | Package metadata, scripts, and entry-point configuration |
| `tsconfig.json` | TypeScript compiler settings |
Prerequisites
Assume that you can read basic TypeScript, use npm, and understand import and export. You should also know how to run the TypeScript compiler with tsc.
Keep source and build output separate
Do not publish your src/ directory as the primary implementation that consumers execute. Source files contain TypeScript syntax, while most Node.js consumers need JavaScript. The build step converts the source into files in dist/.
For example, a small slug library might use this layout:
slugify/
├── src/
│ ├── index.ts
│ └── slugify.ts
├── dist/
├── package.json
├── tsconfig.json
└── README.mdThe dist/ directory is generated. Add it to .gitignore if your project does not commit build output. The published package must still include it, because consumers need the generated files.
Define one public entry point
The public entry point is the file that gathers the library's supported exports. Internal files can change without being part of the package's public contract.
// src/slugify.ts
export interface SlugOptions {
separator?: string;
}
export function slugify(
input: string,
options: SlugOptions = {},
): string {
const separator = options.separator ?? "-";
return input
.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, separator)
.replace(new RegExp(`^${separator}+|${separator}+$`, "g"), "");
}// src/index.ts
export { slugify } from "./slugify.js";
export type { SlugOptions } from "./slugify.js";The .js extension in the import is intentional. With Node-compatible ESM settings, TypeScript checks the source import and maps it to the emitted JavaScript file. The generated dist/index.js will import ./slugify.js, which exists at runtime.
The export type statement publishes the type without creating a runtime JavaScript import. This keeps the generated module focused on values that exist when the program runs.
Why the boundary matters
If consumers import dist/slugify.js directly, that path becomes an accidental API. A later file move can break them. Encourage this usage instead:
import { slugify } from "@acme/slugify";
console.log(slugify("Hello, World!"));Later chapters configure package.json so this package name resolves to the public entry point and so only intended paths are importable.
Exercise
Why should a library expose src/index.ts as a public entry point instead of asking consumers to import individual implementation files?
Show answer
The entry point defines a stable API boundary. Internal files can be reorganized without promising that their paths and private details will remain unchanged.