.d.ts Declaration Files

Last updated: 2026-10-22

.d.ts Declaration Files

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.

GroupEngineering & Practice
KindConfig
Strict modeNot 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 / letDeclares a value provided by the host (a library, a CDN global, ...); the type comes from your description
declare functionDeclares 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)

💡 Notes

ℹ️ 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.
More cases coming soon.

📚 Related tutorials

❓ FAQ

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.com

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.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏