From a6df584973fe7890ba2a627c46a7e0ffdb48edf9 Mon Sep 17 00:00:00 2001 From: Adam Duren Date: Mon, 12 Nov 2018 15:29:37 -0800 Subject: [PATCH] Implement demo of using JSDoc to support type checking js --- package.json | 7 +++- src/browser/PluginGeocoder.js | 8 ++++ tsconfig.json | 14 +++++++ typings/index.d.ts | 69 +++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 tsconfig.json create mode 100644 typings/index.d.ts diff --git a/package.json b/package.json index d08a4a9ed..c7614911b 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,8 @@ } ], "scripts": { - "test": "jest && npm run eslint", + "check-types": "tsc", + "test": "npm run check-types && npm run jest && npm run eslint", "jest": "jest", "test:watch": "jest --watch", "eslint": "node node_modules/eslint/bin/eslint www src" @@ -55,8 +56,10 @@ }, "homepage": "https://github.com/mapsplugin/cordova-plugin-googlemaps", "devDependencies": { + "@types/googlemaps": "^3.30.16", "cordova-js": "^4.2.4", "eslint": "^5.7.0", - "jest": "^23.6.0" + "jest": "^23.6.0", + "typescript": "^3.1.6" } } diff --git a/src/browser/PluginGeocoder.js b/src/browser/PluginGeocoder.js index 7ae1506d6..47aae5db7 100644 --- a/src/browser/PluginGeocoder.js +++ b/src/browser/PluginGeocoder.js @@ -1,5 +1,6 @@ +// @ts-ignore var BaseArrayClass = require('cordova-plugin-googlemaps.BaseArrayClass'); var geocoder = null; @@ -146,6 +147,12 @@ QUEUE.on('next', function() { }); module.exports = { + /** + * + * @param {(result: GeocoderResult[]) => void} onSuccess + * @param {(err: string) => void} onError + * @param {[GeocoderRequest]} args + */ 'geocode': function(onSuccess, onError, args) { var request = args[0]; var geocoderRequest = {}; @@ -176,4 +183,5 @@ module.exports = { }; +// @ts-ignore require('cordova/exec/proxy').add('PluginGeocoder', module.exports); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 000000000..431013ffc --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "allowJs": true, + "checkJs": true, + "noEmit": true, + "esModuleInterop": true + }, + "include": [ + "src/browser/PluginGeocoder.js", + "typings/**/*.d.ts" + ], +} \ No newline at end of file diff --git a/typings/index.d.ts b/typings/index.d.ts new file mode 100644 index 000000000..a3089fdbf --- /dev/null +++ b/typings/index.d.ts @@ -0,0 +1,69 @@ +interface ILatLng { + lat: number; + lng: number; +} + +interface ILatLngBounds { + northeast: ILatLng; + southwest: ILatLng; +} + +interface GeocoderRequest { + /** + * The address property or position property is required. + * You can not specify both property at the same time. + * + * [geocoding usage1] + * let request: GeocoderRequest = { + * address: "Los Angeles, California, USA" + * } + * + * [geocoding usage2] + * let request: GeocoderRequest = { + * address: [ + * "Los Angeles, California, USA", + * "San Francisco, California, USA", + * ] + * } + */ + address?: string | string[]; + /** + * + * [reverse-geocoding usage1] + * let request: GeocoderRequest = { + * position: {"lat": 37.421655, "lng": -122.085637} + * } + * + * [reverse-geocoding usage2] + * let request: GeocoderRequest = { + * position: [ + * {"lat": 37.421655, "lng": -122.085637}, + * {"lat": 37.332, "lng": -122.030781} + * ] + * } + */ + position?: ILatLng | ILatLng[]; + + bounds?: ILatLng | ILatLng[]; +} + +interface GeocoderResult { + adminArea?: string; + country?: string; + countryCode?: string; + extra?: { + featureName?: string; + lines?: string[]; + permises?: string; + phone?: string; + url?: string; + }; + locale?: string; + locality?: string; + position?: ILatLng; + postalCode?: string; + subAdminArea?: string; + subLocality?: string; + subThoroughfare?: string; + thoroughfare?: string; +}