根据您对 JohnnyHK 的回答,您可以执行以下操作吗?
如果您现有的 jquery.d.ts 看起来像这样:
interface JQuery {
//...OTHER MEMBERS IN JQUERY
addClass(classNames: string): JQuery;
addClass(func: (index: any, currentClass: any) => JQuery);
attr(attributeName: string): string;
attr(attributeName: string, value: any): JQuery;
//...OTHER MEMBERS IN JQUERY
}
interface JQueryStatic {
//...OTHER MEMBERS IN JQUERY
parent(selector?: string): JQuery;
parents(selector?: string): JQuery;
parentsUntil(selector?: string, filter?: string): JQuery;
parentsUntil(element?: Element, filter?: string): JQuery;
//...OTHER MEMBERS IN JQUERY
}
declare var jQuery: JQueryStatic;
declare var $: JQueryStatic;
然后,您需要在代码中某处做的就是定义扩展方法,例如:
文件1.ts
///<reference path='jquery.d.ts'/>
interface JQuery {
myNewExtensionMethod() : string;
}
interface JQueryStatic {
myNewExtensionMethod() : string;
}
jQuery.myNewExtensionMethod = () => {
return "11";
}
$.myNewExtensionMethod = () => {
return "11";
}
然后使用它们:
文件2.ts
///<reference path='file1.ts'/>
var result = $.myNewExtensionMethod();