From 19fcce5bfead2b17dd91381459cd7c0b7aee1713 Mon Sep 17 00:00:00 2001 From: breeze9527 Date: Sat, 13 Apr 2019 19:16:54 +0800 Subject: [PATCH] Add defition of non npm package amap-js-api-city-search --- .../amap-js-api-city-search-tests.ts | 63 +++++++++++++++++ types/amap-js-api-city-search/index.d.ts | 67 +++++++++++++++++++ types/amap-js-api-city-search/tsconfig.json | 24 +++++++ types/amap-js-api-city-search/tslint.json | 3 + 4 files changed, 157 insertions(+) create mode 100644 types/amap-js-api-city-search/amap-js-api-city-search-tests.ts create mode 100644 types/amap-js-api-city-search/index.d.ts create mode 100644 types/amap-js-api-city-search/tsconfig.json create mode 100644 types/amap-js-api-city-search/tslint.json diff --git a/types/amap-js-api-city-search/amap-js-api-city-search-tests.ts b/types/amap-js-api-city-search/amap-js-api-city-search-tests.ts new file mode 100644 index 0000000000..fade4da0ab --- /dev/null +++ b/types/amap-js-api-city-search/amap-js-api-city-search-tests.ts @@ -0,0 +1,63 @@ +// $ExpectType CitySearch +const citySearch = new AMap.CitySearch(); + +citySearch.getLocalCity((status, result) => { + const statusTemp: 'error' | 'complete' | 'no_data' = status; + if (typeof result !== 'string') { + // $ExpectType SearchResult + result; + // $ExpectType string + result.adcode; + // $ExpectType Bounds + result.bounds; + // $ExpectType string + result.city; + // $ExpectType string + result.info; + // $ExpectType string + result.infocode; + // $ExpectType string + result.province; + // $ExpectType string + result.rectangle; + // $ExpectType string + result.status; + } else { + // $ExpectType string + result; + } +}); + +// $ExpectType void +citySearch.getCityByIp('ip', (status, result) => { + const statusTemp: 'error' | 'complete' | 'no_data' = status; + const resultTemp: AMap.CitySearch.SearchResult | string = result; +}); + +citySearch.on('complete', (event: AMap.CitySearch.EventMap['complete']) => { + // $ExpectType "complete" + event.type; + // $ExpectType string + event.adcode; + // $ExpectType Bounds + event.bounds; + // $ExpectType string + event.city; + // $ExpectType string + event.info; + // $ExpectType string + event.infocode; + // $ExpectType string + event.province; + // $ExpectType string + event.rectangle; + // $ExpectType string + event.status; +}); + +citySearch.on('error', (event: AMap.CitySearch.EventMap['error']) => { + // $ExpectType string + event.info; + // $ExpectType "error" + event.type; +}); diff --git a/types/amap-js-api-city-search/index.d.ts b/types/amap-js-api-city-search/index.d.ts new file mode 100644 index 0000000000..1044125864 --- /dev/null +++ b/types/amap-js-api-city-search/index.d.ts @@ -0,0 +1,67 @@ +// Type definitions for non-npm package amap-js-api-city-search 1.4 +// Project: https://lbs.amap.com/api/javascript-api/reference/location#m_AMap.CitySearch +// Definitions by: breeze9527 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +/// + +declare namespace AMap { + namespace CitySearch { + interface EventMap { + complete: Event<'complete', SearchResult>; + error: Event<'error', { info: string }>; + } + interface SearchResult { + /** + * 城市名称 + */ + city: string; + rectangle: string; + /** + * 城市范围 + */ + bounds: Bounds; + /** + * 成功信息 + */ + info: string; + /** + * 地区编号 + */ + adcode: string; + /** + * 查询信息码 + */ + infocode: string; + /** + * 省份 + */ + province: string; + /** + * 查询状态 + */ + status: string; + } + type SearchStatus = 'error' | 'complete' | 'no_data'; + } + /** + * 根据IP返回对应城市信息 + */ + class CitySearch extends EventEmitter { + /** + * 自动获取用户IP,回调返回当前用户所在城市 + * @param callback 查询回调 + */ + getLocalCity(callback: (status: CitySearch.SearchStatus, result: CitySearch.SearchResult | string) => void): void; + /** + * 根据输入IP地址返回对应城市信息 + * @param ip IP + * @param callback 查询回调 + */ + getCityByIp( + ip: string, + callback: (status: CitySearch.SearchStatus, result: CitySearch.SearchResult | string) => void + ): void; + } +} diff --git a/types/amap-js-api-city-search/tsconfig.json b/types/amap-js-api-city-search/tsconfig.json new file mode 100644 index 0000000000..e28900c85b --- /dev/null +++ b/types/amap-js-api-city-search/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noEmit": true, + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "amap-js-api-city-search-tests.ts" + ] +} diff --git a/types/amap-js-api-city-search/tslint.json b/types/amap-js-api-city-search/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/amap-js-api-city-search/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}