Is it possible to model jQuery.fn.extend using TypeScript
My guess is that the answer to this is "no" but I wanted to check there wasn't something I'd missed.
jQuery has a fn.extend method which allows you to augment the jQuery object with extra methods. Here's an example from the API docs:
jQuery.fn.extend({
check: function() {
return this.each(function() {
this.checked = true;
});
},
uncheck: function() {
return this.each(function() {
this.checked = false;
});
}
});
// Use the newly created .check() method
$( "input[type='checkbox']" ).check();
I've been filling out the jQuery JSDoc and tests for the DefinitelyTyped project. Previously jQuery.fn was defined as any
. To bring it in line with the docs I've been looking at changing it to cover the extend
method:
fn: {
/**
* Merge the contents of an object onto the jQuery prototype to provide new jQuery instance methods.
*
* @param object An object to merge onto the jQuery prototype.
*/
extend(object: any): any;
}
However, I don't think there's a way in TypeScript to model the dynamic rather than static augmentation of methods. This means it's not possible to subsequently use these dynamically augmented methods in your TS unless you either:
any
or $("input[type='checkbox']")["check"]();
So my question: is there something I've missed? Is there a better way to model jQuery.fn
so TypeScript can pick up that jQueryStatic
gets assigned these dynamic methods? As I say, I think the answer is "no" but I wanted to be sure.
It is a definitive no.
It would be nice to execute code in the compiler inside the definition but no such syntax exists. This is what I wish it had:
interface Foo{
bar(member:any):any @ execute { // execute this code inside the compiler language service:
var keys = compiler.getArgs().keys();
compiler.getIdentifier('JQueryStatic').addMembers /// you get the point.
}
}
But it has the potential of making the compilation slow, and would be full of edge cases, not to mention its more work that isn't on the table I presume.
链接地址: http://www.djcxy.com/p/18554.html