blog icon indicating copy to clipboard operation
blog copied to clipboard

`.d.ts/.ts/declare` 三者的关系

Open nmsn opened this issue 3 years ago • 0 comments

  • .ts 使用 typescript 开发使用的文件类型
  • .d.ts ts 的声明文件
  • declare 声明修饰符

当我们想使用 typescript 开发时只需要使用 .ts 后缀文件开发即可,不需要特别编写 .d.ts 文件或者使用 declare 修饰符。

当我们使用 tsc --declaration 或者在 tsconfig.json 中配置 declaration:true 时会生成 xxx.ts 对应的 xxx.d.ts 文件和 xxx.js

xxx.js 是编译后的 js 文件,xxx.d.ts 是其对应的 ts 声明文件,其中用 declare 声明我们在 .ts 中使用 ts 编写的代码。

这样,即使我们引入的代码来自于 xxx.js 编辑器也能帮我们从 xxx.d.ts 中帮我们识别引入内容的类型。

nmsn avatar Aug 12 '22 09:08 nmsn