4

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

4
  • q2: stackoverflow.com/questions/26540165/… Commented Feb 10, 2017 at 0:17
  • I have downloaded a definition jquery definition file and referenced it in my .ts file, this works without erros, but compile on save for ts files does currently not work yet... Commented Feb 10, 2017 at 1:02
  • 1
    While there is some conceptual overlap between the Visual Studio settings and what's in a tsconfig file, they have different scopes and purposes. You absolutely need a tsconfig file for any project that has more than a dozen files and for an AMD (RequireJS) project of even the smallest imaginable size. Commented Feb 10, 2017 at 2:47
  • I've added the tsconfig, set AllowJS to false, in order to limit the error output to only a few lines, I get a lot of bunch of "Duplicate ..." errors.. Commented Feb 10, 2017 at 9:45

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.