简体   繁体   English

在打字稿中使用声明文件比源文件有什么优势吗?

[英]Is there any advantage of using declaration files over sources in typescript?

When creating and publishing a library using typescript there are 2 options:使用打字稿创建和发布库时,有两个选项:

  1. Generate declaration files d.ts together with the bundled javascript file and then in package.json point to it with:生成声明文件d.ts与捆绑的 javascript 文件,然后在package.json指向它:

    "types": "./dist/mylib.d.ts" “类型”:“./dist/mylib.d.ts”

  2. Don't generate the declaration files and point directly to the source code in package.json witt:不要生成声明文件,直接指向package.json witt 中的源代码:

    "types": "./src/index.ts" “类型”:“./src/index.ts”

What is the adventage of each one?每个人的优势是什么?

I suggest the following strategy:我建议以下策略:

Library Package图书馆套餐

In the library package, distribute only the complied JavaScript with the declarations files.在库包中,仅分发带有声明文件的已编译 JavaScript。 This has the advantage of reducing the package size while giving developers the option to use your library with either JavaScript or TypeScript.这具有减少包大小的优势,同时让开发人员可以选择将您的库与 JavaScript 或 TypeScript 一起使用。

Code Repository代码库

In the code repository for the library, only commit the source files - not the compiled JavaScript nor the declarations files.在库的代码库中,只提交源文件——而不是编译的 JavaScript 和声明文件。 Put instructions for how to compile the library in your README, and perhaps also a script that will perform the compilation.在你的自述文件中放置关于如何编译库的说明,也许还有一个将执行编译的脚本。 This will allow other developers to change and use your library after any modifications they make.这将允许其他开发人员在进行任何修改后更改和使用您的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM