I'm testing typescript with jquery, but when I compile the test.ts file, it always gives me an error indicating: Cannot find name '$'.
I've already imported jquery & added its definition reference.
If I use import $ = require("jquery")
in my test.ts
file, another error "Cannot find module jquery
" will occur when doing the tsc
compiling. However, the JQuery folder already exists within the node_modules folder.
Does anyone know what is the correct way to use jquery in typescript?
Below is my steps:
- Install jquery using
npm install jquery --save
- Install typings & jquery definition using
typings install --global --save dt~jquery
- Add jquery reference at top of test.ts
/// <reference path="../../../typings/globals/jquery/index.d.ts" />
tsconfig.json
{
"compilerOptions": {
"jsx": "react",
"outDir": "./dist",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"experimentalDecorators": true
},
"exclude": [
"node_modules"
],
"files": [
"./typings/index.d.ts",
"./src/wo/tests/test.ts",
]
}
test.ts
/// <reference path="../../../typings/globals/jquery/index.d.ts" />
let test:any=$("div");
tsconfig.json
definition is wrong. You can't use bothexclude
andfiles
(files
will win in this case, so maybe path is wrong). Also you don't need/// <reference
iftsconfig.json
used