Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#60724 [leaflet] Fix overload order for eve…
Browse files Browse the repository at this point in the history
…nts by @MysteryBlokHed

* Fix overload order for events

Fixes the overload order for event-related functions so that the most
generic ones come last.

* Update tests

Updates existing tests for event functions to see if the correct event
type is inferred.

* Update inconsistent formatting in tests
  • Loading branch information
MysteryBlokHed authored Jun 9, 2022
1 parent 0282ba9 commit 80633f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
12 changes: 6 additions & 6 deletions types/leaflet/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ export abstract class Evented extends Class {
* (e.g. 'click dblclick').
*/
// tslint:disable:unified-signatures
on(type: string, fn: LeafletEventHandlerFn, context?: any): this;
on(type: 'baselayerchange' | 'overlayadd' | 'overlayremove',
fn: LayersControlEventHandlerFn, context?: any): this;
on(type: 'layeradd' | 'layerremove',
Expand Down Expand Up @@ -400,6 +399,7 @@ export abstract class Evented extends Class {
fn: TileEventHandlerFn, context?: any): this;
on(type: 'tileerror',
fn: TileErrorEventHandlerFn, context?: any): this;
on(type: string, fn: LeafletEventHandlerFn, context?: any): this;

/**
* Adds a set of type/listener pairs, e.g. {click: onClick, mousemove: onMouseMove}
Expand All @@ -414,7 +414,6 @@ export abstract class Evented extends Class {
* to off in order to remove the listener.
*/
// tslint:disable:unified-signatures
off(type: string, fn?: LeafletEventHandlerFn, context?: any): this;
off(type: 'baselayerchange' | 'overlayadd' | 'overlayremove',
fn?: LayersControlEventHandlerFn, context?: any): this;
off(type: 'layeradd' | 'layerremove',
Expand Down Expand Up @@ -447,6 +446,7 @@ export abstract class Evented extends Class {
fn?: TileEventHandlerFn, context?: any): this;
off(type: 'tileerror',
fn?: TileErrorEventHandlerFn, context?: any): this;
off(type: string, fn?: LeafletEventHandlerFn, context?: any): this;

/**
* Removes a set of type/listener pairs.
Expand Down Expand Up @@ -476,7 +476,6 @@ export abstract class Evented extends Class {
* Behaves as on(...), except the listener will only get fired once and then removed.
*/
// tslint:disable:unified-signatures
once(type: string, fn: LeafletEventHandlerFn, context?: any): this;
once(type: 'baselayerchange' | 'overlayadd' | 'overlayremove',
fn: LayersControlEventHandlerFn, context?: any): this;
once(type: 'layeradd' | 'layerremove',
Expand Down Expand Up @@ -509,6 +508,7 @@ export abstract class Evented extends Class {
fn: TileEventHandlerFn, context?: any): this;
once(type: 'tileerror',
fn: TileEventHandlerFn, context?: any): this;
once(type: string, fn: LeafletEventHandlerFn, context?: any): this;

/**
* Behaves as on(...), except the listener will only get fired once and then removed.
Expand All @@ -535,7 +535,6 @@ export abstract class Evented extends Class {
* (e.g. 'click dblclick').
*/
// tslint:disable:unified-signatures
addEventListener(type: string, fn: LeafletEventHandlerFn, context?: any): this;
addEventListener(type: 'baselayerchange' | 'overlayadd' | 'overlayremove',
fn: LayersControlEventHandlerFn, context?: any): this;
addEventListener(type: 'layeradd' | 'layerremove',
Expand Down Expand Up @@ -568,6 +567,7 @@ export abstract class Evented extends Class {
fn: TileEventHandlerFn, context?: any): this;
addEventListener(type: 'tileerror',
fn: TileErrorEventHandlerFn, context?: any): this;
addEventListener(type: string, fn: LeafletEventHandlerFn, context?: any): this;

/**
* Alias for on(...)
Expand All @@ -586,7 +586,6 @@ export abstract class Evented extends Class {
* to off in order to remove the listener.
*/
// tslint:disable:unified-signatures
removeEventListener(type: string, fn?: LeafletEventHandlerFn, context?: any): this;
removeEventListener(type: 'baselayerchange' | 'overlayadd' | 'overlayremove',
fn?: LayersControlEventHandlerFn, context?: any): this;
removeEventListener(type: 'layeradd' | 'layerremove',
Expand Down Expand Up @@ -619,6 +618,7 @@ export abstract class Evented extends Class {
fn?: TileEventHandlerFn, context?: any): this;
removeEventListener(type: 'tileerror',
fn?: TileErrorEventHandlerFn, context?: any): this;
removeEventListener(type: string, fn?: LeafletEventHandlerFn, context?: any): this;

/**
* Alias for off(...)
Expand All @@ -641,7 +641,6 @@ export abstract class Evented extends Class {
* Behaves as on(...), except the listener will only get fired once and then removed.
*/
// tslint:disable:unified-signatures
addOneTimeEventListener(type: string, fn: LeafletEventHandlerFn, context?: any): this;
addOneTimeEventListener(type: 'baselayerchange' | 'overlayadd' | 'overlayremove',
fn: LayersControlEventHandlerFn, context?: any): this;
addOneTimeEventListener(type: 'layeradd' | 'layerremove',
Expand Down Expand Up @@ -674,6 +673,7 @@ export abstract class Evented extends Class {
fn: TileEventHandlerFn, context?: any): this;
addOneTimeEventListener(type: 'tileerror',
fn: TileErrorEventHandlerFn, context?: any): this;
addOneTimeEventListener(type: string, fn: LeafletEventHandlerFn, context?: any): this;

/**
* Alias for once(...)
Expand Down
20 changes: 15 additions & 5 deletions types/leaflet/leaflet-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,30 @@ map = new L.Map(htmlElement, mapOptions);
let doesItHaveLayer: boolean;
doesItHaveLayer = map.hasLayer(L.tileLayer(''));

map.on('zoomanim', (_e: L.ZoomAnimEvent) => {});
map.on('zoomanim', e => {
e; // $ExpectType ZoomAnimEvent
});

map.once({
dragend: (_e: L.DragEndEvent) => {},
locationfound: (_e: L.LocationEvent) => {},
});

map.off('moveend');
map.off('resize', (_e: L.ResizeEvent) => {});
map.off('baselayerchange', (_e: L.LayersControlEvent) => {}, {});
map.off('resize', e => {
e; // $ExpectType ResizeEvent
});
map.off('baselayerchange', e => {
e; // $ExpectType LayersControlEvent
}, {});

map.removeEventListener('loading');
map.removeEventListener('dblclick', (_e: L.LeafletMouseEvent) => {});
map.removeEventListener('locationerror', (_e: L.ErrorEvent) => {}, {});
map.removeEventListener('dblclick', e => {
e; // $ExpectType LeafletMouseEvent
});
map.removeEventListener('locationerror', e => {
e; // $ExpectType ErrorEvent
}, {});

map.panInside(latLng, { padding: [50, 50], paddingBottomRight: point, paddingTopLeft: [100, 100] });
map.panInside(latLng, { padding: [50, 50], paddingBottomRight: point, paddingTopLeft: [100, 100], animate: true, duration: 0.5 });
Expand Down

0 comments on commit 80633f8

Please sign in to comment.