TypeScript和库,例如jQuery(带.d.ts文件) [英] TypeScript and libraries such as jQuery (with .d.ts files)

查看:1934
本文介绍了TypeScript和库,例如jQuery(带.d.ts文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了整个互联网,但我还没有找到一个全面的指南,告诉我如何使用像jquery这样的库并在TypeScript项目中使用它。如果有一个指南存在,我很想知道在哪里这些是我真正需要知道的事情:

I've looked all over the internets but I have yet to find a comprehensive guide that tells me how to take a library such as jquery and use it in a TypeScript project. If there is a guide that exists I would love to know where otherwise these are the things I really need to know:


  1. 我明白.d.ts文件仅用于intellisense所以我需要什么才能让jquery实际工作(编译成ts?)

  2. 我需要需要 //参考如果是,那实际上是做什么的?

  3. 任何事情要从这些.d.ts和jquery js文件在我的ts项目中使用库(或任何库)。

  1. I understand that the .d.ts file is only for intellisense so what do I need to get jquery to actually work (compile to ts?)
  2. Do I need a requires or a //reference when using VS2013 and if so what is that actually doing?
  3. Anything to go from these .d.ts and jquery js files to using the library (or any library) in my ts project.

我去过能够解决其他所有问题,但这个问题相当令人沮丧。

I've been able to figure pretty much everything else out but this one has been rather frustrating.

推荐答案

你不需要编译jquery来typescript,你只需要使用一个定义文件来告诉Typescript JavaScript库是如何工作的。

You don't need to compile jquery to typescript, you just need to use a definition file that tells Typescript how the JavaScript library works.

在这里获取定义:
https://github.co m / DefinitelyTyped / DefinitelyTyped

或者如果使用Visual Studio则来自NuGet。

or from NuGet if using Visual Studio.

然后只写您的打字稿正常,并在需要时声明您的库:

Then just write your typescript as normal, and declare your library if needed:

声明var library:libraryTypedName

例如jquery的d.ts文件已经为你做了这个(查看底部):

for example jquery's d.ts file already does this for you (check the bottom):

declare module "jquery" {
    export = $;
}
declare var jQuery: JQueryStatic;
declare var $: JQueryStatic;

https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/jquery

现在在 .ts 文件中键入 $ 时,它应该为您提供打字稿intellisense。

Now in your .ts file when you type $ it should give you the typescript intellisense.

现在您想要包含在bundleconfig / < script> 中的唯一内容是.js文件,包括你的和jquery /其他库。打字稿只是编译时间!

Now the only things you want to include in your bundleconfig / <script> are the .js files, both yours and jquery / other libraries. Typescript is COMPILE time only!

这篇关于TypeScript和库,例如jQuery(带.d.ts文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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