Skip to content
jshacks.dev
Packages

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.

1 min read
Summarize in AI

The smallest publishable TypeScript library needs a source file, a compiler config and a manifest that points at the output.

src/index.tsTypeScript
export function add(a: number, b: number): number {
  return a + b;
}
tsconfig.jsonJSON
{
  "compilerOptions": {
    "target": "es2022",
    "module": "nodenext",
    "rootDir": "src",
    "outDir": "dist",
    "declaration": true,
    "strict": true
  },
  "include": ["src"]
}
package.jsonJSON
{
  "name": "my-lib",
  "version": "1.0.0",
  "type": "module",
  "exports": {
    ".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" }
  },
  "files": ["dist"]
}
Shell
npx tsc
ls dist
Text
index.d.ts  index.js

The 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.

Share

More snippets

Packages

Initialize a package with npm init

Initialize a package.json with npm init, skip the questions with the yes flag, and set your own defaults for author and license in npm config.

1 min read
Packages

Generate a changelog from commits

Turn conventional commit messages into a CHANGELOG.md section with conventional-changelog, grouped by feature and fix. Works with any Git history.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.