Building and Publishing Typed Packages
Building and Publishing Typed Packages — the core idea
A typed package needs a real JavaScript entry point and matching .d.ts declarations. Configure package.json exports so consumers resolve both runtime code and types. Test the package as a consumer before publishing; code that type-checks inside the package can still be packaged incorrectly.
What you’ll learn
- Explain building and publishing typed packages with a concrete use case.
- Run the example and account for every line of its output.
- Recognize the difference between compile-time checking and runtime behavior.
Understand the rules and when to use them
Reading the code: The public Item type describes each input; sum is a runtime export. Declaration emit can produce sum(items: Item[]): number for consumers, but package.json must still point to compiled JavaScript.
Keep the boundary clear: A types entry that points to a file outside the published package causes consumer type errors, even when local builds succeed.
See the idea as a flow
Run the complete TypeScript example
Save the code as building-publishing-typed-packages.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.
TypeScript code
export type Item = { name: string; amount: number };
export function sum(items: Item[]): number { return items.reduce((total, item) => total + item.amount, 0); }
console.log(sum([{ name: "Book", amount: 20 }, { name: "Pen", amount: 5 }]));Verified output
25Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable building-publishing-typed-packages.ts; run: node building-publishing-typed-packages.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.
Trace the example line by line
The public Item type describes each input; sum is a runtime export. Declaration emit can produce sum(items: Item[]): number for consumers, but package.json must still point to compiled JavaScript.
The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.
Build the real multi-file example
This separate mini-project demonstrates real imports, exports, or declarations across files. Create each file in one folder, then compile and run main.js. The example was checked separately from the single-file program.
math.ts
export type Item={cost:number};export function sum(items:Item[]):number{return items.reduce((n,x)=>n+x.cost,0);}
main.ts
import {sum} from "./math";console.log(sum([{cost:4},{cost:6}]));
CommonJS setup: compile with npx tsc --strict --target es2022 --module commonjs main.ts and run node main.js. Compile from the same directory so relative modules are discovered. If this example contains helpers.js, keep that runtime file alongside the emitted main.js.
Verified output from the mini-project
10Avoid this common mistake
A types entry that points to a file outside the published package causes consumer type errors, even when local builds succeed.
Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.
Practice independently
Compile with --declaration, inspect emitted files, and test importing the package from a separate project.
Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.
Check your understanding
What to remember
A typed package needs a real JavaScript entry point and matching .
Try the practice change and re-run the exact program to test your understanding.
Reaching the recap and completing the quizzes are separate milestones.