是否有充分的理由在打字稿项目中将编译器选项 "declaration" 设置为 true

Is there any good reason to set the compiler option "declaration" to true in a typescript project

我有一个 typescript 项目,以后可能想将它发布为 NPM 包。

现在我已经在 tsconfig.json 中设置了 "declaration": true。 然而,这给我带来了一些问题(这对这个问题来说并不重要)。

如果我将其设置为 false,一切都会如我所愿。

declaration 标志设置为真时,生成 d.ts 个文件。不过根据我的理解 post About "*.d.ts" in TypeScript 是,这仅在您尚未使用打字稿进行编程时才相关(即您的项目是 javascript),以便您稍后可以在打字稿中轻松使用此 javascript 库项目,因为您现在有了类型。

那么,当项目是 "pure" typescript 时,为什么还要将此设置设置为 true?我可以安全地将它设置为 false 吗?

Is there any good reason to set the compiler option “declaration” to true in a typescript project?

是的。

特别是如果您要发布为 NPM 包!

理由

创建 NPM 程序包时,您应该包含 .js.d.ts 文件,而不是 .ts 文件。这使您的包真正可移植,并确保它可以被 TypeScript 项目 普通旧 JavaScript 项目使用。

这是 NPM 上推荐的 TypeScript 打包机制,更多关于发布的信息可以在 handbook.

中找到