简体   繁体   English

如何在 Typescript 中实现声明 .d.ts 文件

[英]How to implement declaration .d.ts file in Typescript

I found a lot of literature on what the declaration .d.ts files are, but not on how to implement them.我找到了很多关于声明.d.ts文件是什么的文献,但没有关于如何实现它们。 I understood that they can be auto-generated with the typescript compiler.我知道它们可以使用打字稿编译器自动生成。

So my questions are:所以我的问题是:

  • when should I write the .d.ts file my own, and when should I autogenerate them?我什么时候应该自己编写.d.ts文件,什么时候应该自动生成它们?

  • what happen if I write my own .d.ts files and then also autogenerate them with the compiler?如果我编写自己的.d.ts文件,然后用编译器自动生成它们,会发生什么?

  • should I write a .d.ts file for every .ts file or only to what I will expose as API of my library?我应该为每个.ts文件编写一个.d.ts文件,还是只为我将作为库的 API 公开的内容编写一个.d.ts文件?

Thank you谢谢

If you are creating a library with TypeScript I would suggest leaving it to the TS compiler to generate the definition files.如果您使用 TypeScript 创建一个库,我建议将它留给 TS 编译器来生成定义文件。 By default the compiler creates a d.ts file next to each .ts file.默认情况下,编译器会在每个 .ts 文件旁边创建一个 d.ts 文件。

Typically you would only need to hand craft a .d.ts file when you are using a library that doesn't include type definitions for example some old JavaScript library and you can't find one that someone else has created published to the DefinitelyTyped repo.通常,当您使用不包含类型定义的库(例如一些旧的 JavaScript 库)时,您只需要手工制作一个 .d.ts 文件,并且您找不到其他人创建的发布到绝对类型存储库的库.

If you are writing your own I would suggest you would create a single file.如果您正在编写自己的文件,我建议您创建一个文件。

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

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