为什么用Typescript-Angular2输入定义(.d.ts)? [英] Why type definition (.d.ts) with Typescript - Angular2?

查看:189
本文介绍了为什么用Typescript-Angular2输入定义(.d.ts)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经见过其他SO问题.但是仍然与我的问题相混淆.

我看到了很多使用Jquery(js)Toastr(js) 烤面包机而无需键入和其他示例的示例直接在angular2 app中的JS库,而无需使用其类型定义 (.d.ts)文件,它们可以正常工作.

所以,

1) 基本上什么是类型定义文件的得与失?

I have already seen other SO questions. But still confused with my question.

I have seen many examples which use Jquery(js), Toastr(js) toastr without typings and other JS library directly in angular2 app without using their type definition (.d.ts) files and they work just fine.

So,

1) basically what is gain or lose, going and not going with type definition file?

2) Angular2文件也是javascript文件.那他们的类型定义文件呢 ?

2) Angular2 files are also javascript files. So what about their type definition files ?

注意:我还没有尝试使用NPM,所以我不知道当您实际安装Angualr2 package时是否会将所有相关的t.ds文件添加到typings文件夹中.

Note : I haven't tried with NPM so I don't know if when you actually install Angualr2 package ,it adds all related t.ds files in typings folder.

推荐答案

Typescript定义文件包含用Java编写的代码的类型信息.

Typescript Definition files contain the type information for code written in Javascript.

JavaScript本身不包含类型信息,因此Typescript无法神奇地检索该信息.为了解决该问题,将创建定义文件,该文件告诉Typescript在何处使用什么类型.

Javascript doesn't contain type information itself, so Typescript can't magically retrieve that information. To solve that problem Definition files are created which tell Typescript what types are being used where.

因此,您可以使用不带定义文件的第三方库,但是您会失去Typescript提供的类型安全性.

So, you can use 3rd party libraries without a Definition file, but you'll lose out on the type safety that Typescript offers.

例如,如果您包含jQuery定义文件,则您的IDE现在可以为jQuery API提供智能/自动建议.当您尝试将布尔值传递给需要字符串的函数时,Typescript编译器也会向您发出警告.

For example, if you include the jQuery definition file then your IDE can now provide intellisense/auto-suggestions for the jQuery API. Also the Typescript compiler can give you a warning when you try to pass a boolean to a function that expects a string.

因此,Typescript不需要工作定义文件,但没有它们,您将失去Typescript提供的许多好处.

So definition files aren't required for Typescript to work, but without them you lose a lot of the benefits Typescript provides.

这篇关于为什么用Typescript-Angular2输入定义(.d.ts)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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