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 '$'.
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
Share Improve this question edited Jan 31, 2020 at 0:03 Legends asked Feb 10, 2017 at 0:13 LegendsLegends 22.7k17 gold badges101 silver badges132 bronze badges 4myfile.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 '$'.
- q2: stackoverflow./questions/26540165/… – CM Kanode 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 pile on save for ts files does currently not work yet... – Legends 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 atsconfig
file for any project that has more than a dozen files and for an AMD (RequireJS) project of even the smallest imaginable size. – Aluan Haddad 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.. – Legends Commented Feb 10, 2017 at 9:45
1 Answer
Reset to default 10- Install Node.js if you haven't already
- Open the mand line (cmd.exe)
cd
to your project root directory- 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 $.