Legends
Legends

Reputation: 22702

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 '$'.

Upvotes: 4

Views: 3816

Answers (1)

Legends
Legends

Reputation: 22702

  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 $.

Upvotes: 10

Related Questions