如何在 TypeScript 中实现 jQuery 自动完成?

如果使用的是 TypeScript。当我输入 $字符时,我可以做什么来获得 jQuery 智能感知/自动完成?

(我在 VS 2012的一个 ASP.NET MVC 3项目中工作。)

39308 次浏览

You need to add a reference to the jQuery definition at the top of your .ts file.

/// <reference path="jquery.d.ts" />

You can find type annotations for jQuery in this sample.

Checklist of things that need to be set up for this to work:

  1. Ensure the TypeScript extension is installed. Check Tools -> Extension Manager and make sure "TypeScript for Microsoft Visual Studio" extension is installed. If it isn't, run the .vsix file found in the TypeScript folder (Program Files\Microsoft SDKs\TypeScript\<version>) to install the extension.

  2. Ensure your file references the jquery.d.ts file. You should have a comment at/near the top of your file that looks like this:

/// <reference path="the/path/to/jquery.d.ts" />

If you need to download it, you can find the jquery.d.ts file as part of the Warship sample at http://www.typescriptlang.org/Samples/

Check out these type definitions for TypeScript: https://github.com/borisyankov/DefinitelyTyped#readme. They're being updated regularly and are more complete than the ones in the examples. On Nuget: http://www.nuget.org/profiles/DefinitelyTyped/