From 2c814e16ad78f7e5ad08291d13fa4a32e09ae218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20S=C3=A1nchez?= Date: Mon, 3 Sep 2018 11:54:48 -0600 Subject: [PATCH] improve leaflet Util and Class typings --- types/leaflet/index.d.ts | 20 +++++++++++++------- types/leaflet/leaflet-tests.ts | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/types/leaflet/index.d.ts b/types/leaflet/index.d.ts index 0822dea605..8402d8bc9b 100644 --- a/types/leaflet/index.d.ts +++ b/types/leaflet/index.d.ts @@ -11,10 +11,12 @@ export as namespace L; import * as geojson from 'geojson'; export class Class { - static extend(props: any): any/* how to return constructor of self extended type ? */; - static include(props: any): any /* how to return self extended type ? */; - static mergeOptions(props: any): any /* how to return self extended type ? */; - static addInitHook(initHookFn: () => void): any/* how to return self extended type ? */; + static extend(props: any): {new(...args: any[]): any} & typeof Class; + static include(props: any): any & typeof Class; + static mergeOptions(props: any): any & typeof Class; + + static addInitHook(initHookFn: () => void): any & typeof Class; + static addInitHook(methodName: string, ...args: any[]): any & typeof Class; } export class Transformation { @@ -1532,8 +1534,12 @@ export namespace Browser { } export namespace Util { - function extend(dest: any, src?: any): any; - function create(proto: any, properties?: any): any; + function extend(dest: D, src?: S1): D & S1; + function extend(dest: D, src1: S1, src2: S2): D & S1 & S2; + function extend(dest: D, src1: S1, src2: S2, src3: S3): D & S1 & S2 & S3; + function extend(dest: any, ...src: any[]): any; + + function create(proto: object | null, properties?: PropertyDescriptorMap): any; function bind(fn: () => void, ...obj: any[]): () => void; function stamp(obj: any): number; function throttle(fn: () => void, time: number, context: any): () => void; @@ -1547,7 +1553,7 @@ export namespace Util { function template(str: string, data: any): string; function isArray(obj: any): boolean; function indexOf(array: any[], el: any): number; - function requestAnimFrame(fn: () => void, context?: any, immediate?: boolean): number; + function requestAnimFrame(fn: (timestamp: number) => void, context?: any, immediate?: boolean): number; function cancelAnimFrame(id: number): void; let lastId: number; diff --git a/types/leaflet/leaflet-tests.ts b/types/leaflet/leaflet-tests.ts index 66f12fd0ed..d1375fa83f 100644 --- a/types/leaflet/leaflet-tests.ts +++ b/types/leaflet/leaflet-tests.ts @@ -444,10 +444,13 @@ defaultIcon = new L.Icon.Default({imagePath: 'apath'}); const myControlClass = L.Control.extend({}); const myControl = new myControlClass(); +const myOtherControlClass = myControlClass.extend({}); +const myOtherControl = new myOtherControlClass(); L.Control.include({}); L.Control.mergeOptions({}); L.Control.addInitHook(() => {}); +L.Control.addInitHook('method1', 'hello', 1); export class MyNewControl extends L.Control { constructor() { @@ -526,8 +529,35 @@ polyline = new L.Polyline(multiPolylineLatLngs); polyline.setLatLngs(multiPolylineLatLngs); const multiPolylineLatLngs2: L.LatLng[][] = polyline.getLatLngs() as L.LatLng[][]; -L.Util.extend({}); +const obj1 = { + prop1: 1, +}; + +const obj2 = { + prop2: '2', +}; + +const obj3 = { + prop3: 'three', +}; + +const obj4 = { + prop4: 'cuatro', +}; + +const obj5 = { + prop5: 'cinque', +}; + +const extended0: typeof obj1 = L.Util.extend(obj1); +const extended1: typeof obj1 & typeof obj2 = L.Util.extend(obj1, obj2); +const extended2: typeof obj1 & typeof obj2 & typeof obj3 = L.Util.extend(obj1, obj2, obj3); +const extended3: typeof obj1 & typeof obj2 & typeof obj3 & typeof obj4 = L.Util.extend(obj1, obj2, obj3, obj4); +const extended4: typeof obj1 & typeof obj2 & typeof obj3 & typeof obj4 & typeof obj5 = L.Util.extend(obj1, obj2, obj3, obj4, obj5); + L.Util.create({}); +L.Util.create(null, {foo: {writable: true, value: 'bar'}}); + L.Util.bind(() => {}, {}); L.Util.stamp({}); L.Util.throttle(() => {}, 123, {}); @@ -546,7 +576,7 @@ L.Util.template('template', {}); L.Util.isArray({}); L.Util.indexOf([], {}); L.Util.requestAnimFrame(() => {}); -L.Util.requestAnimFrame(() => {}, {}); +L.Util.requestAnimFrame(timestamp => console.log(timestamp), {}); L.Util.requestAnimFrame(() => {}, {}, true); L.Util.cancelAnimFrame(1); L.Util.emptyImageUrl;