我的猜测是,答案是“不”,但我想检查一下我没有错过的东西.
jQuery有一个fn.extend method,允许你用额外的方法扩充jQuery对象.以下是api文档中的示例:
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();
我一直在填写jQuery JSDoc并测试DefinitelyTyped项目.以前jQuery.fn被定义为any.为了使它符合我一直在考虑更改它以涵盖扩展方法的文档:
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;
}
但是,我认为TypeScript中没有一种方法可以模拟动态而非静态的方法扩充.这意味着您无法在TS中随后使用这些动态增强的方法,除非您:
>将jQuery转换为任何或
>使用括号表示法,如$(“input [type =’checkBox’]”)[“check”]();
所以我的问题是:有什么我错过了吗?有没有更好的方法来建模jQuery.fn所以TypeScript可以拿起jQueryStatic分配这些动态方法?正如我所说,我认为答案是“不”,但我想确定.
解决方法
这是一个明确的否定.
在定义中的编译器中执行代码会很好,但是不存在这样的语法.这就是我希望它:
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.
}
}
但是它有可能使编译变得缓慢,并且会充满边缘情况,更不用说我认为不在桌面上的更多工作了.