Best way to make jquery ui using typescript

I am creating a jquery widget from a factory widget typed in typescript. How to provide good intellisense without having to write .d.ts?

Example:

/*mywidget.ts*/ $.widget("ui.mywidget", { options: { myoption: "" }, doSomething: function () { this._hasDoSomething = true; /*do doSomething*/ }, hasDoSomething: function (): bool { return <bool>this._hasDoSomething; } }); /*mywidget.d.ts*/ interface MyWidgetOptions { myoption: string; } interface MyWidget extends Widget, MyWidgetOptions {} interface NLIB { mywidget: MyWidget; } interface JQuery { mywidget(): JQuery; mywidget(methodName: string): JQuery; mywidget(options: MyWidgetOptions): JQuery; mywidget(optionLiteral: string, optionName: string): any; mywidget(optionLiteral: string, options: MyWidgetOptions): any; mywidget(optionLiteral: string, optionName: string, optionValue: any): JQuery; } interface JQueryStatic { nlib: NLIB; } /// <reference path="teste.d.ts" /> $(".teste").mywidget({ myoption: "asdadds" }); 

It's too boring to write .d.ts for each plugin. Any alternative?

Note: If I do not include mywidget.d.ts, this code will not compile and I will not have intellisense:

 ///<reference path="path/to/jqueryui-1.9.d.ts"/> $(".teste").mywidget({ myoption: "asdadds" }); 

/ * compiler: ... the property "mywidget" does not exist by value of type "jQuery" * /

+6
source share
2 answers

The GitHub DefinitelyTyped project provides definition files for most popular JavaScript libraries.

All you have to do is upload the file you need and reference it in the code:

 ///<reference path="path/to/jqueryui-1.9.d.ts"/> 
+10
source

If you do not need a .d.ts file, just use the declare keyword in front of your interface. For example, see my answer at fooobar.com/questions/274670 / ...

0
source

All Articles