考虑以下TypeScript文件:
// declare a jquery plugin, so that it can be used in the class
interface JQueryStatic {
plot(arg: any, arg2: any, arg3: any): JQueryStatic;
}
export class Foo {
public plotThings() {
$.plot('#myDiv'); // ERROR HERE
// TS2339 (TS) Property 'plot' does not exist on type 'JQueryStatic'
}
}
因此无法找到该plot
方法。但是,如果我export
从类声明中删除该文件,则可以正常编译。
那么,如何同时拥有JQueryStatic接口(以便能够使用jQuery插件)和导出的类呢?
您不能只扩展在其他地方声明的接口。Typescript为每个文件的所有内容命名空间,因此无法知道jQuery类型定义所声明的全局接口。
因此,创建一个jquery.d.ts
文件以包含您的jQuery特定扩展名并添加此扩展名,以使您清楚表示全局扩展名。
declare global {
interface JQuery {
plot(arg: any, arg2: any, arg3: any): JQuery;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句