简体   繁体   English

为什么用Typescript - Angular2键入定义(.d.ts)?

[英]Why type definition (.d.ts) with Typescript - Angular2?

I have already seen other SO questions. 我已经看过其他SO问题。 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. 我见过许多使用Jquery(js)Toastr(js) toastr没有打字的例子和其他JS库直接在angular2 app而不使用它们的类型定义(.d.ts)文件,它们工作得很好。

So, 所以,

1) basically what is gain or lose, going and not going with type definition file? 1) 基本上什么是获利或失败,去和不去用类型定义文件?

2) Angular2 files are also javascript files. 2) Angular2文件也是javascript文件。 So what about their type definition files ? 那么他们的类型定义文件怎么样?

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. 注: 我没有尝试过与NPM ,所以我不知道,当你实际安装Angualr2 package ,它增加了所有相关t.ds在文件typings的文件夹。

Typescript Definition files contain the type information for code written in Javascript. Typescript Definition文件包含用Javascript编写的代码的类型信息。

Javascript doesn't contain type information itself, so Typescript can't magically retrieve that information. Javascript本身不包含类型信息,因此Typescript无法神奇地检索该信息。 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. 因此,您可以使用没有定义文件的第三方库,但是您将失去Typescript提供的类型安全性。

For example, if you include the jQuery definition file then your IDE can now provide intellisense/auto-suggestions for the jQuery API. 例如,如果包含jQuery定义文件,那么IDE现在可以为jQuery API提供intellisense / auto-advice。 Also the Typescript compiler can give you a warning when you try to pass a boolean to a function that expects a string. 当您尝试将布尔值传递给需要字符串的函数时,Typescript编译器也会发出警告。

So definition files aren't required for Typescript to work, but without them you lose a lot of the benefits Typescript provides. 因此,Typescript不需要定义文件,但没有它们会失去Typescript提供的许多好处。

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

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