1

I'm trying to import jquery into my TypeScript project.

To do this I ran tsd install jquery --save which created me a jquery.d.ts file, and which added /// <reference path="jquery/jquery.d.ts" /> to my tsd.d.ts file.

When adding import * as $ from 'jquery'; to the top of the file I'm working on, I then get 77 compile errors from jquery.d.ts. All are in the form: cannot find name "document". As well as Document, it can't find: Element, Event, Node, HtmlElement, XmlDocument, Text and XMLHttpRequest

See screenshot below for a few examples:

enter image description here

Does anyone have any suggestions as to what I need to do?

3
  • as long as jquery.d.ts is not compiled it doesn't matter. It can file on your IDE but in the end it will compile and works Commented Jun 27, 2016 at 11:24
  • @iberbeu - when compiling with this file in my VS Code IDE, I get: "Build errors have been detected during preLaunchTask 'npm'." and a list of the 77 errors (which are also underlined in my IDE). Commented Jun 27, 2016 at 12:25
  • but jquery.d.ts shouldn't be compiled because your application doesn't need it to run. Try to set it up not to compile jquery.d.ts and then run it Commented Jun 27, 2016 at 12:36

1 Answer 1

1

Have you tried restarting Visual Studio Code (or Reload Window)? I do find that it gets confused quite often when declarations get installed or changed.

The types are defined and come as part of Visual Studio Code and are automatically available within VSCode (you should not have to manually reference them):

enter image description here

Also:

  1. Consider using typings, which is the successor to tsd (you can still consume type declarations from the tsd library - e.g.: typings search jquery and typings install --save --global dt~jquery where dt~ means install from DefinitelyTyped repository)

  2. Consider using a tsconfig.json file to specify the workspace area for TypeScript and exclude the node_modules and the typings/tsd folders

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

3 Comments

+1 for recommending typings over tsd. Do you have any idea why VS Code wouldn't be able to find those types (I've checked I have the file in the location in your image: C:\Program Files (x86)\Microsoft VS Code\resources\app\extensions\typescript\server\typescript\lib\lib.d.ts)
@Andy No idea - sorry. Make sure the file type comes up as "TypeScript" in the status bar
Thank you. Your answer was enough to tell me that something was wrong with my local install. I reinstalled a few things and it's all working now as your answer suggested it should.

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.