简体   繁体   中英

How to reference jQuery (intellisense) in a typescript file (VS2015)

I have a lot of javascript plugins and libraries in my ASP.NET MVC project,
that I want to convert to typescript.

Questions:

How to reference jQuery in ts files?

I get this error below when I manually build the ts file using tsc

myfile.ts(170,4): error TS2304: Cannot find name 'jQuery'.
myfile.ts(172,1): error TS2304: Cannot find name '$'.
myfile.ts(173,2): error TS2304: Cannot find name '$'.

  1. Install Node.js if you haven't already
  2. Open the command line (cmd.exe)
  3. cd to your project root directory
  4. Execute npm install @types/jquery

Now under node_modules you will find the "index.d.ts" typescript definition file for jQuery.

node_modules/@types/jquery/index.d.ts


5. Drag and Drop the file on the top of your opened typescript file "yourFile.ts"

/// <reference path="../../../node_modules/@types/jquery/index.d.ts" />
....
... your typescript code here

Now you will have intellisense for jQuery and $ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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