Create a library with a single entry and types
Build a minimal TypeScript library that ships JavaScript and declaration files, with one exports entry, tsc as the only build tool and TypeScript 5.8.
The smallest publishable TypeScript library needs a source file, a compiler config and a manifest that points at the output.
export function add(a: number, b: number): number {
return a + b;
}{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"rootDir": "src",
"outDir": "dist",
"declaration": true,
"strict": true
},
"include": ["src"]
}{
"name": "my-lib",
"version": "1.0.0",
"type": "module",
"exports": {
".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" }
},
"files": ["dist"]
}npx tsc
ls distindex.d.ts index.jsThe declaration option emits the .d.ts file that consumers' editors read, and exports points both Node.js and TypeScript at the output. See the tsconfig reference for declaration.
Run the build in a prepublishOnly script, so an out-of-date dist is never published. The files field keeps sources, tests and config out of the tarball, so a consumer sees one import path, one set of types and nothing else, which is the right size for a first release.