关于"* .d.ts"在TypeScript中 [英] About "*.d.ts" in TypeScript

查看:246
本文介绍了关于"* .d.ts"在TypeScript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对*.d.ts感到好奇,因为我是TypeScript的新手.有人告诉我,这种文件类似于C ++中的头文件",但仅适用于JS.但是除非将*.js强制更改为*.ts,否则我无法将纯JS文件转换为*.d.ts文件.所以我有三个文件: JS文件,TS文件和*.d.ts文件.

I'm feeling curious about *.d.ts because I'm a newbie in TypeScript. And I was told by someone that this kind of file is something like "head file" in C++ but for JS only. But I cannot convert a pure JS file to *.d.ts file unless I forcely change the *.js to *.ts. So I have three files: a JS file, a TS file and a *.d.ts file.

  1. 它们之间是什么关系?

  1. What's the relationship between them?

如何使用*.d.ts文件?这是否意味着我可以永久删除*.ts文件?

How can I use the *.d.ts file? Does it mean I can delete the *.ts file permanently?

如果是这样,*.d.ts文件如何知道哪个JS文件正在映射到其自身?

If so, how can the *.d.ts file know which JS file is mapping to itself?


如果有人能给我一个例子,那就太好了.


It would be very nice if someone can give me an example.

推荐答案

"d.ts"文件用于提供有关用JavaScript编写的API的打字稿类型信息.想法是您使用的是jQuery或下划线(一个现有的javascript库).您想使用打字稿代码中的代码.

The "d.ts" file is used to provide typescript type information about an API that's written in JavaScript. The idea is that you're using something like jQuery or underscore, an existing javascript library. You want to consume those from your typescript code.

您可以编写仅包含类型注释的d.ts文件,而不是重写jquery或下划线或打字稿中的任何内容.然后,从您的打字稿代码中,您仍然可以使用纯JS库获得静态类型检查的打字稿优势.

Rather than rewriting jquery or underscore or whatever in typescript, you can instead write the d.ts file, which contains only the type annotations. Then from your typescript code you get the typescript benefits of static type checking while still using a pure JS library.

这篇关于关于"* .d.ts"在TypeScript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆