Skip to content

Declaration File

一句话解释

declaration file (.d.ts) provides TypeScript 类型信息 for JavaScript code 那个 doesn't 包含类型, 启用类型检查 for 整个 JS ecosystem.

用大白话说

Declaration files (.d.ts) describe the types of JavaScript libraries. They contain only type information — interfaces, function signatures, type aliases — no implementation code. They are erased at compile time.

当你install @类型/lodash, you are getting a .d.ts file that tells TypeScript how to use Lodash. Without it, TypeScript would treat the library as 任何.

You can write your own .d.ts files for internal JavaScript modules or for libraries that don't ship types. The declare keyword is used to tell TypeScript that the described entity exists at runtime, even though TypeScript can't see its implementation.

示例

typescript
// my-module.d.ts
declare module "my-module" {
  export function doSomething(x: string): number;
  export const version: string;
}

这个 declaration file tells TypeScript 那个 my-模块导出 doSomething and version.

相关术语

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表