A .d.ts file contains only type declarations — function signatures, interfaces, variable types — with no actual implementation. It lets a JavaScript library (which has no types of its own) be used from TypeScript with full autocomplete and compile-time checking. The community-maintained DefinitelyTyped project publishes these as @types/* npm packages for thousands of popular untyped JavaScript libraries.
A basic declaration file
declare tells TypeScript "this exists at runtime, trust me — here's its type" without providing (or requiring) an implementation in this file.
declare function greet(name: string): string;
declare interface Config {
apiKey: string;
timeout?: number;
}
declare const VERSION: string;@types packages: types for existing JavaScript libraries
Installing an @types/* package adds nothing to your runtime bundle — it's a devDependency containing only .d.ts files, which TypeScript reads at compile time and then discards.
$ npm install lodash // the actual JavaScript library
$ npm install -D @types/lodash // its community-maintained type declarations
// now this has full autocomplete and type-checking, even though
// lodash itself is plain untyped JavaScript:
import _ from "lodash";
_.chunk([1, 2, 3, 4], 2);Writing your own for an untyped library
When no @types package exists for a library, a minimal ambient declaration (even just declare module "library-name";, treating it as any) unblocks usage, and can be refined incrementally with real type information as needed.
declare module "some-untyped-library" {
export function doSomething(input: string): number;
}