Declaration files (.d.ts) describe what a library looks like without containing any implementation, so TypeScript can type-check plain JS libraries. Types come from three sources: bundled with the library (best), community @types packages (npm i -D @types/xxx), or hand-written. The core of hand-writing is the declare keyword — it asserts "this value/function is implemented elsewhere" and the compiler emits no code for it.
Group
Engineering & Practice
Kind
Config
Strict mode
Not required
ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.
📝 Syntax
// my-lib.d.ts — declaration file: types only, erased after compilation
declare const APP_VERSION: string;
declare function log(msg: string): void;
interface Config {
host: string;
port: number;
debug?: boolean;
}
// Install community types: npm i -D @types/lodash
⚙️ Parameters
declare const / let
Declares a value provided by the host (a library, a CDN global, ...); the type comes from your description
declare function
Declares a function signature for an existing implementation — types only, no body
@types/*
Community-maintained declaration packages (DefinitelyTyped); install into devDependencies and they take effect automatically
Returns:None — .d.ts files emit no JS after compilation; they only feed type information to the compiler.
▶ Example
Edit the code below and click Try it to see the transpiled result:
Output:
info app booted
[10:24:00] OK db connected
(Runtime output after transpilation; the LogOptions shape comes from the .d.ts — passing { timestamps: "yes" } yields error TS2322)
ℹ️ Info Type resolution priority: the .d.ts pointed to by the library's package.json types field wins, then @types packages, then hand-written declarations in your project.
💡 Tip When hand-writing declarations, run the library once and record the real shape as an interface — far more reliable than guessing from docs; mark uncertain fields optional.
⚠️ Note Declared variables have no backing JS after compilation — the runtime value must come from the host. If nothing assigns LOG_LEVEL, you get undefined at runtime and TypeScript will not warn you.
Q I installed @types/xxx but still get no types — now what?
A Check the package name (@types/ plus the original package name) and your tsconfig include scope; if DefinitelyTyped has nothing, hand-write one: create a .d.ts, describe the APIs you use with declare, and keep it inside the compilation scope (globally or per module).
Q How is declare const different from a normal const?
A A normal const compiles to a real JS variable; declare const is a type-level promise telling the compiler "this name will exist at runtime" — nothing is emitted. That is why declare statements cannot have initializers.
Q What is declare module "xxx" for?
A Ambient module declaration: adds types for an npm package or non-TS asset. Two typical uses — declare module "pkg" { ... } for untyped third-party packages, and declare module "*.css" so style imports stop failing with "cannot find module".
Web-Tutorial Tech Team
A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.