最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to reference jQuery (intellisense) in a typescript file (VS2015) - Stack Overflow

programmeradmin3浏览0评论

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

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

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 4
  • 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 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. – 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
Add a ment  | 

1 Answer 1

Reset to default 10
  1. Install Node.js if you haven't already
  2. Open the mand 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 $.

发布评论

评论列表(0)

  1. 暂无评论