Extend JQueryStatic with typescript

So far, I have added extension methods in jQuery as follows:

module JQueryExtensions {
    export function foo() {
        ...
    }
}
interface JQuery {
    foo(): JQuery;
}
(function ($) {
    $.fn.foo = function (): JQuery {
    ...
    return this;
    };
})(jQuery);

It was good as an extension method for a jQuery instance of type $ ('selector'). foo (). But now I need to add a static function in order to be able to call it as $ .foo ();

I tried replacing "jQuery" with "jQueryStatic" as follows:

module JQueryExtensions {
    export function foo() {
        ...
    }
}
interface JQueryStatic{
    foo(): JQuery;
}
(function ($) {
    $.fn.foo = function (): JQuery {
    ...
    return this;
    };
})(JQueryStatic);

But it gives me the following error: "Cannot find the name" JQueryStatic "."

+4
source share
1 answer

Just pass jQuery(function / type definition) instead of JQueryStatic(interface) and use $.foo =(static method) instead of $.fn.foo =(instance method)

(function ($:JQueryStatic) {
    $.foo = function (): JQuery {
        ...
    };
})(jQuery);
+2
source

Source: https://habr.com/ru/post/1649667/


All Articles