Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add `jquery` to the types field in your tsconfig

I want to use jquery in my Angular-TypeScript project. So I installed jquery like "npm install @type/jquery". Now when I try to serve it says:

error TS2592: Cannot find name '$'. Do you need to install type definitions for jQuery? Try npm i @types/jquery and then add jquery to the types field in your tsconfig.

How do I add jquery to the types field?

My Code:

let settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://api.com/getSomething",
  "method": "POST",
  "headers": {
    "x-rapidapi-host": "xxx",
    "x-rapidapi-key": "xxx",
    "content-type": "xxx"
  },
  "data": {}
}

$.ajax(settings).done(function (response) {
  console.log(response);
});
like image 346
Julius Avatar asked Jan 26 '23 06:01

Julius


1 Answers

It's very well explained in the error message. If you want to use TS, and use jQuery, you'll need the typings for jQuery. You can achieve this by running npm i @types/jquery in your project.

You will then need to add "jquery" in the types array in your tsconfig.app.json file, in the Angular application (might depend on your Angular version - best to check in the documentation).

Might I add - if you're using Angular, I'd say it's best not to use jQuery. Angular is already capable of doing everything. If you'd like to use jQuery, having an entire Angular application with it is a bit unnecessary.

like image 198
saglamcem Avatar answered Jan 27 '23 18:01

saglamcem