From bed07e18cac15a36a60186fe97afa356b517eac0 Mon Sep 17 00:00:00 2001 From: breeze9527 Date: Sat, 20 Apr 2019 03:58:51 +0800 Subject: [PATCH] Add definition for non npm package: amap-js-api-district-search (#34856) * [amap-js-api] update to v1.4.14 * Add definition for non npm package: amap-js-api-district-search --- .../amap-js-api-district-search-tests.ts | 71 ++++++++++++ types/amap-js-api-district-search/index.d.ts | 108 ++++++++++++++++++ .../amap-js-api-district-search/tsconfig.json | 24 ++++ types/amap-js-api-district-search/tslint.json | 3 + 4 files changed, 206 insertions(+) create mode 100644 types/amap-js-api-district-search/amap-js-api-district-search-tests.ts create mode 100644 types/amap-js-api-district-search/index.d.ts create mode 100644 types/amap-js-api-district-search/tsconfig.json create mode 100644 types/amap-js-api-district-search/tslint.json diff --git a/types/amap-js-api-district-search/amap-js-api-district-search-tests.ts b/types/amap-js-api-district-search/amap-js-api-district-search-tests.ts new file mode 100644 index 0000000000..19ee65c238 --- /dev/null +++ b/types/amap-js-api-district-search/amap-js-api-district-search-tests.ts @@ -0,0 +1,71 @@ +// $ExpectType DistrictSearch +new AMap.DistrictSearch(); +// $ExpectType DistrictSearch +new AMap.DistrictSearch({}); +// $ExpectType DistrictSearch +const districtSearch = new AMap.DistrictSearch({ + level: 'city', + showbiz: true, + extensions: 'all', + subdistrict: 1 +}); + +// $ExpectType void +districtSearch.search('keyword', (status, result) => { + const temp: 'error' | 'complete' | 'no_data' = status; + // $ExpectType string | SearchResult + result; + if (typeof result !== 'string') { + // $ExpectType string + result.info; + // $ExpectType District[] + result.districtList; + { + const district = result.districtList[0]; + // $ExpectType string + district.adcode; + // $ExpectType LngLat[][] | undefined + district.boundaries; + // $ExpectType LngLat + district.center; + // $ExpectType string + district.citycode; + // $ExpectType District[] | undefined + district.districtList; + // $ExpectType Level + district.level; + // $ExpectType string + district.name; + } + } else { + // $ExpectType string + result; + } +}); + +declare const level: 'country' | 'province' | 'city' | 'district' | 'biz_area'; +// $ExpectType void +districtSearch.setLevel(level); +districtSearch.setLevel(); + +// $ExpectType void +districtSearch.setSubdistrict(3); + +// $ExpectError +districtSearch.setSubdistrict(4); + +districtSearch.on('complete', (event: AMap.DistrictSearch.EventMap['complete']) => { + // $ExpectType "complete" + event.type; + // $ExpectType string + event.info; + // $ExpectType District[] + event.districtList; +}); + +districtSearch.on('error', (event: AMap.DistrictSearch.EventMap['error']) => { + // $ExpectType "error" + event.type; + // $ExpectType string + event.info; +}); diff --git a/types/amap-js-api-district-search/index.d.ts b/types/amap-js-api-district-search/index.d.ts new file mode 100644 index 0000000000..967d43ae45 --- /dev/null +++ b/types/amap-js-api-district-search/index.d.ts @@ -0,0 +1,108 @@ +// Type definitions for non-npm package amap-js-api-district-search 1.4 +// Project: https://lbs.amap.com/api/javascript-api/reference/search#m_AMap.DistrictSearch +// Definitions by: breeze9527 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +/// + +declare namespace AMap { + namespace DistrictSearch { + interface EventMap { + error: Event<'error', { info: string; }>; + complete: Event<'complete', SearchResult>; + } + type Level = 'country' | 'province' | 'city' | 'district' | 'biz_area'; + interface Options { + /** + * 关键字对应的行政区级别或商圈 + */ + level?: Level; + /** + * 是否显示商圈 + */ + showbiz?: boolean; + /** + * 是否返回行政区边界坐标点 + */ + extensions?: 'base' | 'all'; + /** + * 显示下级行政区级数 + * 0:不返回下级行政区 + * 1:返回下一级行政区 + * 2:返回下两级行政区 + * 3:返回下三级行政区 + */ + subdistrict?: 0 | 1 | 2 | 3; + } + interface District { + /** + * 行政区名称 + */ + name: string; + /** + * 中心点经纬度坐标 + */ + center: LngLat; + /** + * 城市编码 + */ + citycode: string; + /** + * 区域编码 + */ + adcode: string; + /** + * 行政区划级别 + */ + level: Level; + /** + * 边界坐标集合 + */ + boundaries?: LngLat[][]; + /** + * 下级行政区信息列表 + */ + districtList?: District[]; + } + interface SearchResult { + /** + * 成功状态文字描述 + */ + info: string; + /** + * 行政区划列表 + */ + districtList: District[]; + } + type SearchStatus = 'complete' | 'error' | 'no_data'; + } + class DistrictSearch extends EventEmitter { + /** + * 行政区查询 + * @param options 选项 + */ + constructor(options?: DistrictSearch.Options); + /** + * 根据关键字查询行政区或商圈信息 + * @param keyword 关键词 + * @param callback 回调 + */ + search( + keyword: string, + callback: (status: DistrictSearch.SearchStatus, result: DistrictSearch.SearchResult | string) => void + ): void; + /** + * 设置关键字对应的行政区级别或商圈 + * @param level 级别 + */ + setLevel(level?: DistrictSearch.Level): void; + /** + * 设置下级行政区级数 + * @param district 级数 + */ + setSubdistrict(district?: 0 | 1 | 2 | 3): void; + // internal + setExtensions(extensions?: boolean): void; + } +} diff --git a/types/amap-js-api-district-search/tsconfig.json b/types/amap-js-api-district-search/tsconfig.json new file mode 100644 index 0000000000..8783e6ee84 --- /dev/null +++ b/types/amap-js-api-district-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-district-search-tests.ts" + ] +} diff --git a/types/amap-js-api-district-search/tslint.json b/types/amap-js-api-district-search/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/amap-js-api-district-search/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}