rajdeep26
rajdeep26

Reputation: 51

Property 'select2' does not exist on type 'JQuery<HTMLElement>'

I am using Angular 4 with select2 plugin. I have already added @types for jquery and select2. Vscode intellisense shows as a method select2 and doesnt throw any error, but angular-cli's compilation fails.

Upvotes: 4

Views: 4100

Answers (2)

Val
Val

Reputation: 22797

In my case, that's because I imported another library (sweetalert2), with the wrong jQuery definition.

In sweetalert2.d.ts line 885:

/**
 * These interfaces aren't provided by SweetAlert2, but its definitions use them.
 * They will be merged with 'true' definitions.
 */

// tslint:disable:no-empty-interface

interface JQuery {
}

This override JQuery definition with the wrong result (which originally should be JQuery<T = HTMLElement>). After removing it everything runs correctly.

Upvotes: 0

alcfeoh
alcfeoh

Reputation: 2267

I had a similar issue and fixed it by importing:

import 'select2';

which I had installed earlier:

npm install select2 --save

Once you do that, you can use the library through jQuery as usual, and your IDE won't complain about it.

Upvotes: 2

Related Questions