From 021cf2a985ff11b7885f3c0cc97ae9894e176785 Mon Sep 17 00:00:00 2001 From: Andrew Pinkham Date: Wed, 24 Jan 2018 12:01:32 -0500 Subject: [PATCH] Fix Leaflet.Draw.DrawEvents.Created.layer type (#23137) The DrawEvents.Created layer variable is not of type Layer. As noted by the in-code documentation, the Leaflet.Draw.DrawEvents.Created.layer variable is one of Leaflet's vector shapes or a markers. https://github.com/Leaflet/Leaflet.draw/blob/c6af99b761c5d9900bf5a3e2e1d8827bbde29542/src/Leaflet.Draw.Event.js#L31 The L.Draw.Event.CREATED event is only fired by Draw.Feature in the _fireCreatedEvent method. Draw.Feature bequeaths _fireCreatedEvent to it's subclasses Draw.Marker, Draw.Polyline, and Draw.SimpleShape, which in turn bequeath the method to Draw.Circle, Draw.Rectangle (both from Draw.SimpleShape), Draw.CircleMarker (from Draw.Marker), and Draw.Polygon (from Draw.Polyline). Draw.Circle, Draw.CircleMarker, Draw.Marker, Draw.Rectangle and Draw.Polyline override the _fireCreatedEvent to pass instances of their respective vector classes to _fireCreatedEvent, meaning that the layer variable is not of type Layer, but instead one of the vector or marker classes. For example, Draw.Circle passes L.Circle to L.Draw.Feature.prototype._fireCreatedEvent as the argument to the `layer` parameter. Note that Draw.Polygon inherits Draw.Polyline's behavior without changes, but changes the vector class via the Poly class variable. --- types/leaflet-draw/index.d.ts | 2 +- types/leaflet-draw/leaflet-draw-tests.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/types/leaflet-draw/index.d.ts b/types/leaflet-draw/index.d.ts index 35d014b699..7fd33f2152 100644 --- a/types/leaflet-draw/index.d.ts +++ b/types/leaflet-draw/index.d.ts @@ -387,7 +387,7 @@ declare module 'leaflet' { /** * Layer that was just created. */ - layer: Layer; + layer: Circle | CircleMarker | Marker | Polygon | Polyline | Rectangle; /** * The type of layer this is. One of: polyline, polygon, rectangle, circle, marker. diff --git a/types/leaflet-draw/leaflet-draw-tests.ts b/types/leaflet-draw/leaflet-draw-tests.ts index e63006edce..54015fa404 100644 --- a/types/leaflet-draw/leaflet-draw-tests.ts +++ b/types/leaflet-draw/leaflet-draw-tests.ts @@ -41,6 +41,7 @@ map.addControl(drawControl); map.on(L.Draw.Event.CREATED, (e: L.DrawEvents.Created) => { const type = e.layerType; const layer = e.layer; + const geojson = e.layer.toGeoJSON(); drawnItems.addLayer(layer); });