4

So far i have added extension methods to JQuery like this:

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

It was good as an extension method to a JQuery instance like $('selector').foo(). But now i need to add a static function to be able to call it like $.foo();

I Have tried replacing "JQuery" with "JQueryStatic" like this:

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 name 'JQueryStatic'."

2
  • just pass jQuery instead of JQueryStatic and use $.foo = instead of $.fn.foo = Commented Jul 31, 2016 at 6:40
  • Thank you @Aleksey, That Worked. Post it as an answer please. Commented Aug 1, 2016 at 4:29

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.