How to integrate jQuery with TypeScript?

How to integrate jQuery with TypeScript?

I tried using jQuery in my TypeScript project with the following code:

$(function(){ 
    alert('Hello'); 
});

However, Visual Studio throws an error: (TS) Cannot find name ‘$’. How can I properly use jQuery with TypeScript and resolve this issue?

Install jQuery Types : npm install --save @types/jquery

After this, the error should be resolved, and TypeScript will recognize $.

This should help you :slight_smile:

You can also try to import Statement :

import * as $ from 'jquery';
$(function() {
  alert('Hello');
});

@shashank_watak @ishrth_fathima, you can also declare the variable in the global scope

Declare jQuery in Global Scope :
declare var $: any;
$(function() {
  alert('Hello');
});

This will tell TypeScript to treat $ as a global variable.