【问题标题】:Extending existing @types type definition to include missing properties扩展现有的 @types 类型定义以包含缺少的属性
【发布时间】:2018-08-01 03:05:19
【问题描述】:

我在我的项目中使用最新的传单@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 存储库中,但与此同时,为了解除对开发的阻碍,我希望我可以使用一些临时的东西

【问题讨论】:

    标签: typescript leaflet typescript-typings


    【解决方案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),它们实际上包含此方法,并且您发布的定义与当前定义中的不同。确保你有最新的定义

    【讨论】:

    • 谢谢,我忘了说我也在使用leaflet-editable,它在他们的@types 文件中也声明了这个LayerGroup,但还没有setZIndex 函数。有趣的是,如果我在编辑器(VS 代码)中转到 L.LayerGroup 的定义文件,我会转到可编辑的传单 index.d.ts 文件,而不是传单文件。似乎我的类型定义发生了某种冲突?
    • @mindparse 您可以从leaflet 模块显式导入接口。 import 'leaflet' import 'leaflet-editable' import { ILayer, LayerGroup } from 'leaflet'; let d: LayerGroup&lt;ILayer&gt;; // from leaflet not leaflet-editable
    猜你喜欢
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多