TypeScript "tsd install jquery" - cannot find name "document"

1.7k Views Asked by At

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?

1

There are 1 best solutions below

3
Ivan Zlatev On BEST ANSWER

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