[jquery] How can I stop "property does not exist on type JQuery" syntax errors when using Typescript?

I am only using one line of jQuery in my application:

$("div.printArea").printArea();

But this is giving me a Typescript error:

The property 'printArea' does not exist on type JQuery?

Can someone tell me how I can stop this error appearing?

This question is related to jquery typescript

The answer is


You can also use the ignore syntax instead of using (or better the 'as any') notation:

// @ts-ignore
$("div.printArea").printArea();

You can also write it like this:

let elem: any;
elem = $("div.printArea");
elem.printArea();

I think this is the most readable solution:

($("div.printArea") as any).printArea();

You can cast it to

(<any>$('.selector') ).function();

Ex: date picker initialize using jquery

(<any>$('.datepicker') ).datepicker();

For your example, you'd add this:

interface JQuery{
    printArea():void;
}

Edit: oops, basarat is correct below. I'm not sure why I thought it was compiling but I've updated this answer.


Since printArea is a jQuery plugin it is not included in jquery.d.ts.

You need to create a jquery.printArea.ts definition file.

If you create a complete definition file for the plugin you may want to submit it to DefinitelyTyped.