Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何定义自定义传单控件的类型_Typescript_Leaflet - Fatal编程技术网

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,所以你不再需要黑客了。