Sub topic 12
Checking a TypeScript Package Before Release
Assume you know TypeScript, npm, package.json, and basic ESM import syntax. This chapter focuses on verification: checking what npm will publish, checking package metadata, checking type resolution, and testing the package from a separate consumer project.
A package can compile successfully and still fail for users. Common problems include missing declaration files, incorrect paths in exports, unpublished build output, and types that work in the package source but not after installation.
Check the files npm will publish
The files field controls which files npm includes. For example:
{
"files": ["dist"]
}This publishes the dist directory while excluding source files unless another npm rule includes them. Run a dry-run before publishing:
npm pack --dry-runThe command lists the files that would be placed in the package tarball. Confirm that the list includes:
- JavaScript entry points
.d.tsdeclaration files- Source maps, if you publish them
- Files required by documented subpath exports
package.json
It should not include tests, local configuration, credentials, or large development-only files.
Validate package metadata with publint
publint checks whether common package fields agree with one another. It can find problems such as an exports target that does not exist, an invalid main path, or a package that points to the wrong module format.
Run it after building:
npm run build
npx publintTreat warnings as release decisions rather than automatically ignoring them. Some warnings support old tools, while others indicate that a consumer cannot load the package correctly.
Check type resolution with Are the Types Wrong
A package may expose JavaScript correctly while exposing incorrect or incomplete types. Are the Types Wrong checks how TypeScript finds declarations through fields such as exports, types, import, and require.
Run it against the package you intend to publish:
npx @arethetypeswrong/cli --pack .The --pack option checks the package as npm would pack it. This matters because a declaration file present in your working directory might be excluded from the published tarball.
The result should confirm that each supported module condition reaches the matching declaration file. A dual-format package needs separate, compatible paths for ESM and CommonJS. An ESM-only package should not claim to support require.
Test an installed tarball
A separate consumer project catches errors that package-local tests can miss. First create a tarball:
npm run build
npm packCreate a temporary consumer project and install the generated .tgz file:
mkdir package-consumer
cd package-consumer
npm init -y
npm install ../acme-slugify-1.0.0.tgzAdd a TypeScript file that imports only the public API:
import { slugify, type SlugOptions } from "@acme/slugify";
const options: SlugOptions = { separator: "_" };
console.log(slugify("Hello, World!", options));Compile the consumer with strict checking. The import should resolve without manually adding a path or copying declaration files.
flowchart TD
A[Build package] --> B[npm pack --dry-run]
B --> C[Inspect published files]
C --> D[Run publint]
D --> E[Run Are the Types Wrong]
E --> F[Install tarball in test consumer]
F --> G[Compile and run consumer]caption: A release verification sequence for a TypeScript package. alt: A flowchart from building a package through inspecting files, validating metadata and types, and testing an installed tarball.
A practical release checklist
| Check | What it verifies |
|---|---|
| `npm pack --dry-run` | The correct files will be published |
| `publint` | Package metadata and entry points are coherent |
| Are the Types Wrong | Type declarations resolve for supported conditions |
| Test consumer | Real installation and public imports work |
Fix every error before release. Record any intentional warning and its reason so a later package change does not silently reintroduce the problem.
Exercise
Why is a separate consumer project useful when the library already has passing tests?
Show answer
It tests the packed and installed artifact, so it can reveal missing declaration files, incorrect exports paths, and module-resolution problems that source-tree tests may not see.