扩展现有的@types类型定义以包含缺少的属性

时间:2018-02-21 13:44:06

标签: typescript leaflet typescript-typings

我在我的项目中使用最新的传单@types(v1.2.5),但这些传单不符合最新版本的传单(1.3.x)

特别是,他们的LayerGroup接口不包含setZIndex属性,所以我想进一步扩展接口以包含它,以便我可以在我的TS源中调用该函数。

他们对LayerGroup的类型定义目前如下所示:

declare namespace L {

    /**
      * Create a layer group, optionally given an initial set of layers.
      */
    function layerGroup<T extends ILayer>(layers?: T[]): LayerGroup<T>;


    export interface LayerGroupStatic extends ClassStatic {
        /**
          * Create a layer group, optionally given an initial set of layers.
          */
        new<T extends ILayer>(layers?: T[]): LayerGroup<T>;
    }
    export var LayerGroup: LayerGroupStatic;

    export interface LayerGroup<T extends ILayer> extends ILayer {
        /**
          * Adds the group of layers to the map.
          */
        addTo(map: Map): LayerGroup<T>;

        /**
          * Adds a given layer to the group.
          */
        addLayer(layer: T): LayerGroup<T>;

        /**
          * Removes a given layer from the group.
          */
        removeLayer(layer: T): LayerGroup<T>;

        /**
          * Removes a given layer of the given id from the group.
          */
        removeLayer(id: string): LayerGroup<T>;

        /**
          * Returns true if the given layer is currently added to the group.
          */
        hasLayer(layer: T): boolean;

        /**
          * Returns the layer with the given id.
          */
        getLayer(id: string): T;

        /**
          * Returns an array of all the layers added to the group.
          */
        getLayers(): T[];

        /**
          * Removes all the layers from the group.
          */
        clearLayers(): LayerGroup<T>;

        /**
          * Iterates over the layers of the group, optionally specifying context of
          * the iterator function.
          */
        eachLayer(fn: (layer: T) => void, context?: any): LayerGroup<T>;

        /**
          * Returns a GeoJSON representation of the layer group (GeoJSON FeatureCollection).
          * Note: Descendent classes MultiPolygon & MultiPolyLine return `Feature`s, not `FeatureCollection`s
          */
        toGeoJSON(): GeoJSON.FeatureCollection<GeoJSON.GeometryObject>|GeoJSON.Feature<GeoJSON.MultiLineString|GeoJSON.MultiPolygon>;

        ////////////
        ////////////
        /**
          * Should contain code that creates DOM elements for the overlay, adds them
          * to map panes where they should belong and puts listeners on relevant map events.
          * Called on map.addLayer(layer).
          */
        onAdd(map: Map): void;

        /**
          * Should contain all clean up code that removes the overlay's elements from
          * the DOM and removes listeners previously added in onAdd. Called on map.removeLayer(layer).
          */
        onRemove(map: Map): void;
    }
}

我可以在项目中添加另一个类型定义来提供缺少的属性吗?

我知道我可以提出拉动请求在@types回购中添加这个,但与此同时,为了解锁我的开发,我希望我能用一些临时的东西

1 个答案:

答案 0 :(得分:1)

您可以声明模块的扩充:

// leaflet.aug.d.ts
import 'leaflet';
declare module 'leaflet' {
    export interface LayerGroup<P = any> {
        otherProps: number;
        setZIndex (id: number) : Layer[];
    }
}

// Other file
/// <reference path="./leaflet.aug.d.ts" />    
import 'leaflet';
var d: L.LayerGroup;
d.otherProps = 10;
d.setZIndex(10);

注意 我安装了传单类型定义(npm install @types/leaflet),它们确实包含此方法,并且您发布的定义与定义中的当前定义不同。确保您拥有最新的定义