Publishing packages with types
Publish libraries with first-class typings: emit .d.ts with tsc, point package.json to them, handle ESM/CJS exports, and test before publish.
Publishing packages with types is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 2. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 2 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Goal: Publish a package with solid typings. You will emit .d.ts files, wire package.json correctly, support ESM/CJS via exports, and verify with tsc --noEmit in a sample app.
- Emit declarations
- Point
types(or per-entryexports) - Test before publish
Emit declarations
Enable declaration to emit .d.ts. Use a dedicated build config to keep app tsconfig strict and clean.
// tsconfig.build.json
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"declaration": true,
"emitDeclarationOnly": false,
"outDir": "dist",
"rootDir": "src",
"stripInternal": true
},
"include": ["src/**/*"]
}package.json types/exports
Point types at the root declaration. For subpath exports, include a types entry so editors resolve per-module .d.ts.
// package.json
{
"name": "@acme/widgets",
"version": "1.0.0",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./utils": {
"types": "./dist/utils.d.ts",
"import": "./dist/utils.js",
"require": "./dist/utils.cjs"
}
}
}Source vs .d.ts
Ensure your public API is well-typed. Declarations mirror the source signatures so consumers get autocomplete and checking.
// src/index.ts
export interface Widget { id: string; name: string }
export function greet(w: Widget): string { return `Hello, ${w.name}` }
// dist/index.d.ts (emitted)
export interface Widget { id: string; name: string }
export declare function greet(w: Widget): string
Test in consumer
Before publishing, test in a small consumer project with tsc --noEmit. Expect correct errors for invalid usage.
// consumer/tsconfig.json
{
"compilerOptions": { "strict": true, "target": "ES2020", "module": "ESNext" },
"include": ["./src"]
}
// consumer/src/app.ts
import { greet } from "@acme/widgets"
// Type checks in the consumer
console.log(greet({ id: "w1", name: "Ada" }))
// @ts-expect-error: missing name
// console.log(greet({ id: "w1" }))Tips & pitfalls
Tips:
- Prefer subpath
exportswith per-entrytypes. - Keep internal types unexported or
@internal. - Use a
preparescript to build beforenpm publish.
Publishing types check
Quick check: What is the minimal correct setup to expose types?
Recap
Recap: Emit declarations with tsc, wire package.json (types/exports), and verify in a consumer app before publishing.
Frequently asked questions
Is the “Publishing packages with types” lesson free?
Yes — the full text of “Publishing packages with types” is free to read here on the web, and the TypeScript Academy course includes 2 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Publishing packages with types”?
Publish libraries with first-class typings: emit .d.ts with tsc, point package.json to them, handle ESM/CJS exports, and test before publish. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 2, so you can start here or from the beginning and move at your own pace.
How long does the “Publishing packages with types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- .d.ts basics; export type vs export
- Publishing packages with types