11

I need to use a jquery custom plugin in my application and how can i create a custom jquery plugin using typescript. I have googled a lot and i just got below links

http://typescript.codeplex.com/discussions/398401

Define a custom jQuery UI widget in TypeScript

The above links uses plain javascript code, however javascript code will work in typescript but the issue with this is i can't use typescript classes with in this plugin. Is there any way to use typescript classes with in jquery plugin or else is there any other way to create jquery plugin in typescript.

Any suggestions should be appreciated.

3
  • Typescript creates Javascript. So, what are you struggling with? learn.jquery.com/plugins/basic-plugin-creation Commented Oct 25, 2013 at 10:56
  • @WiredPrairie,I know but I have typescript widget i need to convert into plugin. Is there any way to modify the typescript custom widget into typescript custom plugin? Commented Oct 25, 2013 at 11:48
  • What' a "custom widget"? You'll need to use jQuery Plugin authoring techniques. Commented Oct 25, 2013 at 12:12

2 Answers 2

7

There's very little difference between using JavaScript and Typescript when creating a plugin. You'll likely want to grab the jQuery definition for TypeScript from here first and add it to your project (or include it on the command line, etc., depending on your development environment).

Then, just write the plugin and use whatever classes and existing code you want. The example below doesn't do anything other than demonstrate that it's hardly any different from the original JavaScript plugin techniques.

(function($) {
    $.fn.myPlugin = function() {
        // do something
        return this;
    };

    $.fn.myEachPlugin = function(options)  {
        var settings: any = $.extend({
            color: "#ffff00"
        }, options);

        return this.each(function() {
            // do something ...
        });
    };

})(jQuery);
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer uses the 'fat arrow' anonymous function notation four times. In two occasions, the old-style 'function() {' construct needs to be used, because of the way TypeScript handles 'this': see the TypeScript Handbook. Basically, when using the fat arrow syntax, 'this' points to whatever 'this' was outside the anonymous function. In this case, you need the traditional, normal way, so you need: $.fn.myPlugin = function() { and $.fn.myEachPlugin = function(options) {
1

To expand on WiredPrairie's answer, here's a full typescript plugin:

export interface MyOptions {

}

// plugin definition
$.fn.myPlugin = function(this: JQuery, options: MyOptions): JQuery {
    return this.each(function (this: HTMLElement): void {
        // do something
    });
}

// make the plugin available outside this file
declare global {
    interface JQuery {
        myPlugin(options: MyOptions): this;
    }
}

1 Comment

If you add the export interface and declare global objects into a .d.ts file, you can get type acquisition for code completions/parameter info. (works with vscode's intellisense)

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.