تایپاسکریپت - گسترش تعریف نوع موجود @types برای شامل کردن ویژگیهای مفقود شده
من در پروژهام از آخرین نسخهٔ @types برای leaflet (نسخهٔ ۱.۲.۵) استفاده میکنم، اما اینها با آخرین نسخهٔ leaflet (۱.۳.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;
}
}
آیا ممکن است که من یک تعریف نوع دیگر در پروژهام اضافه کنم تا ویژگی گمشده را فراهم کنم؟
میدانم که میتوانم یک pull request برای اضافه کردن این مورد در مخزن @types ارسال کنم، اما در این بین، برای اینکه از توسعه عقب نمانم، امیدوار بودم بتوانم از چیزی موقت استفاده کنم
پاسخ شماره ۱
شما میتوانید یک افزایش را برای ماژول اعلام کنید:
// 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) و در واقع این متد را شامل میشوند و تعریفی که شما پست کردید شبیه به چیزی نیست که در حال حاضر در تعریف وجود دارد. مطمئن شوید که جدیدترین تعریف را دارید
