Declaration Files
How .d.ts files describe types for existing JavaScript, so untyped libraries can still be used safely from TypeScript.
What they are
A .d.ts file contains only type information no actual runtime code, no implementations. It tells TypeScript "here's the shape of this JS code" without needing to rewrite that code in TypeScript.
When both files exist side by side, import { add } from "./math" gets full type-checking and autocomplete, even though math.js itself has no types.
Where they come from
- Bundled with the library many packages ship their own
.d.tsfiles (check for a"types"field in the package'spackage.json). @types/*packages for libraries that don't ship their own types, the community maintains types separately via DefinitelyTyped:
- Auto-generated running
tsc --declarationon a TypeScript project emits matching.d.tsfiles alongside the compiled JS, so other projects can consume it with full types.
Writing your own for an untyped library
If a library has no types available anywhere, you can declare its shape yourself:
Once this file is included in your project (TypeScript picks up any .d.ts file automatically), imports from "some-untyped-library" are typed accordingly.
Ambient declarations
Sometimes you need to describe something that exists globally at runtime but isn't declared anywhere e.g. a variable injected by a <script> tag: