Typescript 如何定义自定义传单控件的类型
在查看Typescript 如何定义自定义传单控件的类型,typescript,leaflet,Typescript,Leaflet,在查看@types/shapel中的类型定义时,您可以看到自定义控件的定义类似于: export namespace Control { ... class Zoom extends Control { constructor(options?: ZoomOptions); options: ZoomOptions; } ... } 但是,通过以下方式创建自定义控件时: declare module 'leaflet' {
@types/shapel
中的类型定义时,您可以看到自定义控件的定义类似于:
export namespace Control {
...
class Zoom extends Control {
constructor(options?: ZoomOptions);
options: ZoomOptions;
}
...
}
但是,通过以下方式创建自定义控件时:
declare module 'leaflet' {
namespace Control {
class CustomControl extends Control {
constructor(options: CustomOptions);
}
}
namespace control {
function customControl(options: CustomOptions): Control.CustomControl;
}
}
L.Control.CustomControl = L.Control.extend({
...
});
抛出类型脚本错误:
Type'(new(…args:any[])=>any)&typeof Class'缺少类型“typeof CustomControl”中的以下属性:缩放、属性、图层、比例等。
这似乎是因为名称空间和类控件
经过了Typescript的声明合并。这会导致CustomControl
需要来自命名空间而不仅仅是类的属性
有没有一种方法可以在不强制键入
任何的情况下修复或绕过它?我们需要为方法“extend”添加更多的键入
在控件声明之前插入此代码
declare module 'leaflet' {
namespace Control {
function extend(props: any): {new(...args: any[]): any} & typeof Control;
}
}
与成员
declare module 'leaflet' {
namespace Control {
function extend<T extends Object>(props: T): {new(...args: any[]): T} & typeof Control;
}
}
声明模块“传单”{
命名空间控件{
函数扩展(props:T):{new(…args:any[]):T}&typeof控件;
}
}
您可以为Handler添加相同的声明我已经在@types中解决了这个问题/leaflet@1.4.5,所以你不再需要黑客了。