0

I try to use the hasData method from jQuery in typescript like this:

$('.my-element').hasData("my-data")

But unfortunately it is not working. The error I got is

(TS) Property 'hasData' does not exist on type 'jQuery'.

The Version is jquery-3.4.1.js.

Can someone help?

tom
  • 9,550
  • 6
  • 30
  • 49

2 Answers2

1

You could cast it to or extend the jquery typing to add your own method.

(<any>$('.my-element')).hasData("my-data");

//Or add your own custom methods

interface JQuery {
    hasData("my-data"):void;
}
utsav tilava
  • 156
  • 1
  • 12
  • Thanks for your answer. Both is working. I tried to append the method to the interface already, I was to impatient. The red error lines in the code needed some time to disappear. – tom Oct 17 '19 at 11:35
  • I'm sorry. Does not work. Error in Browser `TypeError: $(...).hasData is not a function` – tom Oct 17 '19 at 12:26
0

I believe for a full support you should use jQuery's TS declarations, instead of referring pure JS script. Check https://www.npmjs.com/package/@types/jquery

More information here: https://stackoverflow.com/a/32052431/4108884

Samuil Petrov
  • 542
  • 1
  • 13
  • 24