Lesson 4 of 9 · 25m
Declaration Files: The Contract Layer Most Teams Accidentally Write
Understand what `.d.ts` files really mean, when to write them by hand, and how public types become an API compatibility surface.
A declaration file is not "types for documentation." It is a contract the compiler trusts about JavaScript that exists somewhere else. Once you internalize that, most .d.ts confusion disappears. The file describes a runtime artifact. If the description lies, TypeScript does not become more type-safe; it becomes confidently wrong.
Declarations are emitted or authored — know which one you own
For a TypeScript package, most declarations should be emitted:
{"compilerOptions": {"declaration": true, "declarationMap": true}}Hand-authored declarations make sense when modeling untyped JavaScript, describing global augmentation, or intentionally publishing a smaller or cleaner API surface. They are dangerous when they drift from implementation because nothing in the runtime will correct them.
The public type surface is bigger than exported functions
TypeScript declarations include every type reachable from exported APIs. That means private helper types can become compatibility constraints even if you never exported them directly:
export interface UserRecord {
id: string
flags: UserFlags
}
interface UserFlags {
beta: boolean
}Because UserRecord is public, UserFlags is structurally visible through the declaration graph. If you later rename or reshape it, consumers may break. This is why mature libraries review .d.ts output like API documentation, not as compiler exhaust.
Use api-extractor when the contract matters
Microsoft's API Extractor can roll up declarations, report API changes, and trim unintended exports:
api-extractor run --local --verboseA typical report distinguishes public, beta, alpha, and internal surface. You do not need the full ceremony for every package, but if consumers pay money for compatibility, you need something better than "CI passed."
Ambient declarations are global power
declare global {
interface Window {
__ACME_CONFIG__?: { apiBaseUrl: string }
}
}
export {}Ambient declarations affect typechecking without importing a runtime value. That is powerful and therefore easy to abuse. A missing dependency, browser-injected value, or test harness can be conjured into existence. Review them as carefully as runtime globals, because that is exactly what many of them describe.
Field notes
- Never edit generated
.d.tsoutput as a fix. Change the source type or generation settings, or the lie returns on the next build. declare module "pkg"in application code can silence a missing-types problem while hiding the real packaging bug.skipLibCheckreduces noise from dependency declarations; it does not make your own declarations correct.- Type tests are worthwhile for packages whose consumers are other TypeScript teams.
- A declaration-only package needs version discipline just like runtime code. It is software, not annotations.
Was this lesson helpful?