版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaozhi_2016/article/details/67640348
类型定义文件(*.d.ts)
要在TypeScript引用第三方JavaScript库和框架,首先要了解TypeScript的类型定义文件。TypeScript的类型定义文件用来帮助开发者在TypeScript中使用已有的
JavaScript的工具包,如:Jquery。所有的类型定义文件都是以.d.ts结尾的。这个文件实际上就是一个TypeScript模块,它把你要使用的JavaScript工具包里边的工具
以TypeScript的类或者模块的方式暴露(export)出来,供你在你的模块里去import。
如何获得类型定义文件
以Jquery为例:
1. 在github上有公开的项目DefinitelyTyped,里面有大多数会用到的类型定义文件,找到Jquery的类型定义文件index.d.ts下载下来拷贝进项目中,项目就可以用
jquery来写代码了,而且有代码提示。github地址:https://github.com/DefinitelyTyped/DefinitelyTyped
2.还可以用typings工具,这个工具是用来专门安装类型定义文件的。
首先用npm来安装typings工具,安装后,就可以用typings命令查询一个项目、关键字或框架了,用typings命令把需要的第三方库或框架安装上就可以在项目中直接
使用了。
# Install Typings CLI utility. npm install typings --global # Search for definitions. typings search tape # Find a definition by name. typings search --name react # If you use the package as a module: # Install non-global typings (defaults to "npm" source, configurable through `defaultSource` in `.typingsrc`). typings install debug --save # If you use the package through `<script>`, it is part of the environment, or # the module typings are not yet available, try searching and installing with `--global`: typings install dt~mocha --global --save # If you need a specific commit from github. typings install d3=github:DefinitelyTyped/DefinitelyTyped/d3/d3.d.ts#1c05872e7811235f43780b8b596bfd26fe8e7760 --global --save # Search and install by version. typings info env~node --versions typings install [email protected] --global --save # Install typings from a particular source (use `<source>~<name>` or `--source <source>`). typings install env~atom --global --save typings install bluebird --source npm --save # Use `typings/index.d.ts` (in `tsconfig.json` or as a `///` reference). cat typings/index.d.ts