Vienna Vienna - 3 months ago 27
TypeScript Question

Angular2 & Typescript: Error TS2339: Property 'selectpicker' does not exist on type 'ElementFinder'

Unfortunately we have to use old libraries in the current project, that require jQuery. The activation of those happen in the lifecycle hook section "ngAfterViewChecked". Due to fact, that TypeScript cannot resolve it at the right time, it outputs the error:


Error TS2339:
Property 'selectpicker' does not exist
on type 'ElementFinder'.


That is the concerned code block:

ngAfterViewChecked () {
if (this.agency) {
//noinspection TypeScriptUnresolvedFunction
$('.selectpicker').selectpicker();
}
}


I've added the line
//noinspection TypeScriptUnresolvedFunction
before the selectpicker activation, but it did not help.

We have a preconfigured build-system and because of such of errors it "bounces" us. How can I get rid of this problem?

Answer

You have to extend the type definitions of jQuery. Which means merging the jQuery namespace with your custom .d.ts that defined the .selectpicker() method. See the documentation for help:

https://www.typescriptlang.org/docs/handbook/writing-declaration-files.html

Depending on your jQuery definition files this might work:

interface JQuery {
  selectpicker: () => void;
}