2

I am new to this so bear with me. What i am trying to do is getting jquery to work in a typescript code file.

How should i refer to the .d.ts code?

I have tried the following:

import * as $ from "jquery";

this gives me the error: "'jquery' resolves to non-module entity and cannot be imported with this construct"

then i tried:

import $ = require("jquery");

this gives the following error: Import assignment cannot be used when targeting EcmaScript 2015 modules

How do I go about this to make jquery functions work in my typescript code? Are there other ways?

I am using typescript 2.5.2, module system ES2015, EcmaScript 6 and @types/jquery.


EDIT

i tried import * as $ from "../wwwroot/lib/jQuery/dist/jquery.min.js";, now it gives me the error "... has been resolved ... but --allowJs is not set",I dont have a tsconfig.json yet, I probably need it for the --allowJs command, is that right?

8
  • Try to use relative path to jquery: import * as $ from "path/to/jquery"; Commented Sep 17, 2017 at 14:07
  • I am doing import * as jQuery from 'jquery'. It should work if jQuery is installed through NPM. Commented Sep 17, 2017 at 14:07
  • (at least with TypeScript 2.3.4 it works) Commented Sep 17, 2017 at 14:09
  • i have installed it through npm as well, i am gonna try to be more specific with my path Commented Sep 17, 2017 at 14:09
  • i tried import * as $ from "../node_modules/@types/jquery"; and import * as $ from "../node_modules/@types/jquery/index.d.ts"; and it doesnt work either Commented Sep 17, 2017 at 14:14

2 Answers 2

5

add a reference to the Typescript definition file the top of the file.

/// <reference path ="jquery.d.ts"/> 

instead of

import * as $ from "jquery";

According to the DefinitelyTyped wiki:

A TypeScript declaration file is way of defining the types, functions and parameters in an external third-party JavaScript library. By using a declaration file in your TypeScript code will enable Intellisense and type checking against the external library you are using.

jquery.d.ts is a part of the DefinitelyTyped Library found on GitHub. and also you can install via npm.

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

2 Comments

this compiles, but the resulting .js file does not contain a definition of jquery and thus fails to execute
You have to add jquery.js file to page for executing jquery functions.
0

I dont know it this is a "correct way" of doing things, but i use the following line in my typescript project to use jquery.

const jquery = require('jquery');
// and then later in your class
jquery('.someclass').show();

2 Comments

it gives me the error "Cannot find name 'require' "
If you want to use require, you need to declare it: declare function require(moduleName: string): any;

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.