Added type definition for non-npm package amap-js-api-place-search (#34134)

This commit is contained in:
breeze9527 2019-03-26 00:54:31 +08:00 committed by Sheetal Nandi
parent 1e2d5403fc
commit f7a22e9682
4 changed files with 1199 additions and 0 deletions

View File

@ -0,0 +1,435 @@
declare const map: AMap.Map;
declare const div: HTMLElement;
declare const lnglat: AMap.LngLat;
declare const lnglatTuple: [number, number];
declare const bounds: AMap.Bounds;
declare const polygon: AMap.Polygon;
declare const lang: AMap.Lang;
// $ExpectType PlaceSearch
const placeSearch = new AMap.PlaceSearch();
// $ExpectType PlaceSearch
new AMap.PlaceSearch({});
// $ExpectType PlaceSearch
new AMap.PlaceSearch({
city: '深圳',
citylimit: true,
children: 1,
type: '餐饮服务',
lang: 'zh_cn',
pageSize: 10,
pageIndex: 10,
extensions: 'all',
map,
panel: div,
showCover: true,
renderStyle: 'newpc',
autoFitView: true
});
// $ExpectType void
placeSearch.search('keyword', (status, result) => {
const temp: 'error' | 'complete' | 'no_data' = status;
// $ExpectType string | SearchResult
result;
if (typeof result !== 'string') {
// $ExpectType string
result.info;
// $ExpectType PoiList
result.poiList;
// $ExpectType string[] | undefined
result.keywordList;
// $ExpectType CityInfo[] | undefined
result.cityList;
const poiList = result.poiList;
// $ExpectType number
poiList.pageIndex;
// $ExpectType number
poiList.pageSize;
// $ExpectType number
poiList.count;
const poi = poiList.pois[0];
// $ExpectType string
poi.address;
// $ExpectType number
poi.distance;
// $ExpectType string
poi.id;
// $ExpectType LngLat | null
poi.location;
// $ExpectType string
poi.name;
// $ExpectType string
poi.shopinfo;
// $ExpectType string
poi.tel;
// $ExpectType string
poi.type;
if ('website' in poi) {
// $ExpectType string
poi.adcode;
// $ExpectType string
poi.adname;
// $ExpectType string
poi.citycode;
// $ExpectType string
poi.cityname;
// $ExpectType boolean
poi.discount;
// $ExpectType string
poi.email;
// $ExpectType LngLat | null
poi.entr_location;
// $ExpectType LngLat | null
poi.exit_location;
// $ExpectType boolean
poi.groupbuy;
if (poi.indoor_map) {
const indoorData = poi.indoor_data;
// $ExpectType string
indoorData.cpid;
// $ExpectType string
indoorData.floor;
// $ExpectType string
indoorData.truefloor;
}
poi.pcode;
// $ExpectType PoiPhoto[]
poi.photos;
// $ExpectType string
poi.pname;
// $ExpectType string
poi.postcode;
// $ExpectType string
poi.website;
const photo = poi.photos[0];
// $ExpectType string
photo.title;
// $ExpectType string
photo.url;
// $ExpectType Groupbuy[] | undefined
poi.groupbuys;
if (poi.groupbuys) {
const groupbuy = poi.groupbuys[0];
// $ExpectType string
groupbuy.title;
// $ExpectType string
groupbuy.type_code;
// $ExpectType string
groupbuy.type;
// $ExpectType string
groupbuy.detail;
// $ExpectType string
groupbuy.stime;
// $ExpectType string
groupbuy.etime;
// $ExpectType number
groupbuy.count;
// $ExpectType number
groupbuy.sold_num;
// $ExpectType number
groupbuy.original_price;
// $ExpectType number
groupbuy.groupbuy_price;
// $ExpectType number
groupbuy.discount;
// $ExpectType string
groupbuy.ticket_address;
// $ExpectType string
groupbuy.ticket_tel;
// $ExpectType PoiPhoto[]
groupbuy.photos;
// $ExpectType string
groupbuy.url;
// $ExpectType string
groupbuy.provider;
}
// $ExpectType Discount[] | undefined
poi.discounts;
if (poi.discounts) {
const discount = poi.discounts[0];
// $ExpectType string
discount.title;
// $ExpectType string
discount.detail;
// $ExpectType string
discount.start_time;
// $ExpectType string
discount.end_time;
// $ExpectType number
discount.sold_num;
// $ExpectType PoiPhoto[]
discount.photos;
// $ExpectType string
discount.url;
// $ExpectType string
discount.provider;
}
if (poi.deep_type === 'CINEMA') {
// $ExpectType Cinema
const cinema = poi.cinema;
// $ExpectType string
cinema.intro;
// $ExpectType string
cinema.rating;
// $ExpectType string
cinema.deep_src;
// $ExpectType string
cinema.parking;
// $ExpectType string
cinema.opentime_GDF;
// $ExpectType string
cinema.opentime;
// $ExpectType PoiPhoto[]
cinema.photos;
}
if (poi.deep_type === 'DINING') {
// $ExpectType Dining
const dining = poi.dining;
// $ExpectType string
dining.cuisines;
// $ExpectType string
dining.tag;
// $ExpectType string
dining.intro;
// $ExpectType string
dining.rating;
// $ExpectType string
dining.cp_rating;
// $ExpectType string
dining.deep_src;
// $ExpectType string
dining.taste_rating;
// $ExpectType string
dining.environment_rating;
// $ExpectType string
dining.service_rating;
// $ExpectType string
dining.cost;
// $ExpectType string
dining.recommend;
// $ExpectType string
dining.atmosphere;
// $ExpectType string
dining.ordering_wap_url;
// $ExpectType string
dining.ordering_web_url;
// $ExpectType string
dining.ordering_app_url;
// $ExpectType string
dining.opentime_GDF;
// $ExpectType string
dining.opentime;
// $ExpectType string
dining.addition;
// $ExpectType PoiPhoto[]
dining.photos;
}
if (poi.deep_type === 'SCENIC') {
// $ExpectType Scenic
const scenic = poi.scenic;
// $ExpectType string
scenic.intro;
// $ExpectType string
scenic.rating;
// $ExpectType string
scenic.deep_src;
// $ExpectType string
scenic.level;
// $ExpectType string
scenic.price;
// $ExpectType string
scenic.season;
// $ExpectType string
scenic.recommend;
// $ExpectType string
scenic.theme;
// $ExpectType string
scenic.ordering_wap_url;
// $ExpectType string
scenic.ordering_web_url;
// $ExpectType string
scenic.opentime_GDF;
// $ExpectType string
scenic.opentime;
// $ExpectType PoiPhoto[]
scenic.photos;
}
if (poi.deep_type === 'HOTEL') {
// $ExpectType Hotel
const hotel = poi.hotel;
// $ExpectType string
hotel.rating;
// $ExpectType string
hotel.star;
// $ExpectType string
hotel.intro;
// $ExpectType string
hotel.lowest_price;
// $ExpectType string
hotel.faci_rating;
// $ExpectType string
hotel.health_rating;
// $ExpectType string
hotel.environment_rating;
// $ExpectType string
hotel.service_rating;
// $ExpectType string
hotel.traffic;
// $ExpectType string
hotel.addition;
// $ExpectType string
hotel.deep_src;
// $ExpectType PoiPhoto[]
hotel.photos;
}
}
if (result.cityList) {
const city = result.cityList[0];
// $ExpectType string
city.adcode;
// $ExpectType string
city.citycode;
// $ExpectType number
city.count;
// $ExpectType string
city.name;
}
} else {
// $ExpectType string
result;
}
});
// $ExpectType void
placeSearch.searchNearBy('keyword', lnglat, 10, (status, result) => {
const temp: 'error' | 'complete' | 'no_data' = status;
// $ExpectType string | SearchResult
result;
});
// $ExpectType void
placeSearch.searchNearBy('keyword', lnglatTuple, 10, () => { });
// $ExpectType void
placeSearch.searchInBounds('keyword', bounds, (status, result) => {
const temp: 'error' | 'complete' | 'no_data' = status;
// $ExpectType string | SearchResult
result;
});
// $ExpectType void
placeSearch.searchInBounds('keyword', polygon, () => { });
// $ExpectType void
placeSearch.getDetails('id', (status, result) => {
const temp: 'error' | 'complete' | 'no_data' = status;
// $ExpectType string | SearchResult
result;
});
// $ExpectType void
placeSearch.setType('type');
// $ExpectType void
placeSearch.setType();
// $ExpectType void
placeSearch.setCityLimit(true);
// $ExpectType void
placeSearch.setCityLimit();
// $ExpectType void
placeSearch.setPageIndex(1);
// $ExpectType void
placeSearch.setPageIndex();
// $ExpectType void
placeSearch.setPageSize(1);
// $ExpectType void
placeSearch.setPageSize();
// $ExpectType void
placeSearch.setCity('city');
// $ExpectType void
placeSearch.setCity();
// $ExpectType void
placeSearch.setLang(lang);
// $ExpectType void
placeSearch.setLang();
// $ExpectType "zh_cn" | "en" | "zh_en" | undefined
placeSearch.getLang();
// $ExpectType void
placeSearch.clear();
// $ExpectType void
placeSearch.poiOnAMAP({
id: 'id',
});
// $ExpectType void
placeSearch.poiOnAMAP({
location: lnglat,
id: 'id',
name: 'name'
});
// $ExpectType void
placeSearch.detailOnAMAP({
id: 'id',
});
// $ExpectType void
placeSearch.detailOnAMAP({
location: lnglat,
id: 'id',
name: 'name'
});
// $ExpectType void
placeSearch.open();
// $ExpectType void
placeSearch.close();
placeSearch.on('complete', (event: AMap.PlaceSearch.EventMap['complete']) => {
// $ExpectType "complete"
event.type;
// $ExpectType string
event.info;
// $ExpectType PoiList
event.poiList;
// $ExpectType string[] | undefined
event.keywordList;
// $ExpectType CityInfo[] | undefined
event.cityList;
});
placeSearch.on('listElementClick', (event: AMap.PlaceSearch.EventMap['listElementClick']) => {
// $ExpectType MouseEvent
event.event;
// $ExpectType string
event.id;
// $ExpectType number
event.index;
// $ExpectType Marker<any>
event.marker;
// $ExpectType HTMLLIElement
event.listElement;
});
placeSearch.on('markerClick', (event: AMap.PlaceSearch.EventMap['markerClick']) => {
const markerEvent = event.event;
// $ExpectType Marker<any>
markerEvent.target;
// $ExpectType string
event.id;
// $ExpectType number
event.index;
// $ExpectType Marker<any>
event.marker;
// $ExpectType HTMLLIElement
event.listElement;
});

View File

@ -0,0 +1,737 @@
// Type definitions for non-npm package amap-js-api-place-search 1.4
// Project: https://lbs.amap.com/api/javascript-api/reference/search#m_AMap.PlaceSearch
// Definitions by: breeze9527 <https://github.com/breeze9527>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
/// <reference types="amap-js-api" />
declare namespace AMap {
namespace PlaceSearch {
interface EventMap {
complete: Event<'complete', SearchResult>;
error: Event<'error', { info: string }>;
selectChanged: Event<'selectChanged', {
selected: SelectChangeEventData | EventMap['markerClick'] | EventMap['listElementClick'];
lastSelected: SelectChangeEventData | EventMap['markerClick'] | EventMap['listElementClick'] | null;
}>;
listElementClick: SelectChangeEvent<'listElementClick', MouseEvent>;
markerClick: SelectChangeEvent<'markerClick', Marker.EventMap['click']>;
// internal
renderComplete: Event<'renderComplete', {
result: SelectChangeEventData[];
markers: Marker[];
listElements: HTMLElement[];
}>;
infoWindowClick: Event<'infoWindowClick', SelectChangeEventData & {
event: MouseEvent;
infoWindow: InfoWindow;
infoWindowContentDom: HTMLDivElement;
}>;
willClear: Event<'willClear', {
id: string;
index: number;
data: Poi[];
}>;
markerDestoryed: Event<'markerDestoryed', SelectChangeEventData>; // typo in source code
listElementDetroyed: Event<'listElementDetroyed', SelectChangeEventData>; // typo too
}
interface SelectChangeEventData {
/**
* POI的ID
*/
id: string;
/**
*
*/
index: number;
/**
* POI对应的在地图中的Marker对象
*/
marker: Marker;
/**
* POI在结果面板中对应的列表项
*/
listElement: HTMLLIElement;
/**
* POI的信息
*/
data: Poi[];
}
type SelectChangeEvent<N extends string, E> = Event<N, SelectChangeEventData & {
event: E;
}>;
interface PoiPhoto {
/**
*
*/
title: string;
/**
* url
*/
url: string;
}
interface PoiBase {
/**
* ID
*/
id: string;
/**
*
*/
name: string;
/**
*
*/
type: string;
/**
*
*/
location: LngLat | null;
/**
*
*/
address: string;
/**
*
*/
distance: number;
/**
*
*/
tel: string;
shopinfo: string;
children?: any[]; // TODO Array<{location: LngLat | null}>
}
interface Groupbuy {
/**
*
*/
title: string;
/**
*
*/
type_code: string;
/**
*
*/
type: string;
/**
*
*/
detail: string;
/**
*
*/
stime: string;
/**
*
*/
etime: string;
/**
*
*/
count: number;
/**
*
*/
sold_num: number;
/**
*
*/
original_price: number;
/**
*
*/
groupbuy_price: number;
/**
*
*/
discount: number;
/**
*
*/
ticket_address: string;
/**
*
*/
ticket_tel: string;
/**
*
*/
photos: PoiPhoto[];
/**
* url
*/
url: string;
/**
*
*/
provider: string;
}
interface Discount {
/**
*
*/
title: string;
/**
*
*/
detail: string;
/**
*
*/
start_time: string;
/**
*
*/
end_time: string;
/**
*
*/
sold_num: number;
/**
*
*/
photos: PoiPhoto[];
/**
* url
*/
url: string;
/**
*
*/
provider: string;
}
interface Cinema {
/**
*
*/
intro: string;
/**
*
*/
rating: string;
/**
*
*/
deep_src: string;
/**
*
*/
parking: string;
/**
*
*/
opentime_GDF: string;
/**
*
*/
opentime: string;
/**
*
*/
photos: PoiPhoto[];
}
interface Dining {
/**
*
*/
cuisines: string;
/**
*
*/
tag: string;
/**
*
*/
intro: string;
/**
*
*/
rating: string;
/**
*
*/
cp_rating: string;
/**
*
*/
deep_src: string;
/**
*
*/
taste_rating: string;
/**
*
*/
environment_rating: string;
/**
*
*/
service_rating: string;
/**
*
*/
cost: string;
/**
*
*/
recommend: string;
/**
*
*/
atmosphere: string;
/**
* wap链接
*/
ordering_wap_url: string;
/**
* web链接
*/
ordering_web_url: string;
/**
* APP URL
*/
ordering_app_url: string;
/**
*
*/
opentime_GDF: string;
/**
*
*/
opentime: string;
/**
*
*/
addition: string;
/**
*
*/
photos: PoiPhoto[];
}
interface Scenic {
/**
*
*/
intro: string;
/**
*
*/
rating: string;
/**
*
*/
deep_src: string;
/**
*
*/
level: string;
/**
*
*/
price: string;
/**
*
*/
season: string;
/**
*
*/
recommend: string;
/**
*
*/
theme: string;
/**
* wap购票链接
*/
ordering_wap_url: string;
/**
* web购票链接
*/
ordering_web_url: string;
/**
*
*/
opentime_GDF: string;
/**
*
*/
opentime: string;
/**
*
*/
photos: PoiPhoto[];
}
interface Hotel {
/**
*
*/
rating: string;
/**
*
*/
star: string;
/**
*
*/
intro: string;
/**
*
*/
lowest_price: string;
/**
*
*/
faci_rating: string;
/**
*
*/
health_rating: string;
/**
*
*/
environment_rating: string;
/**
*
*/
service_rating: string;
/**
*
*/
traffic: string;
/**
*
*/
addition: string;
/**
*
*/
deep_src: string;
/**
*
*/
photos: PoiPhoto[];
}
type PoiExt = PoiBase & {
/**
*
*/
website: string;
/**
*
*/
pcode: string;
/**
*
*/
citycode: string;
/**
*
*/
adcode: string;
/**
*
*/
postcode: string;
/**
*
*/
pname: string;
/**
*
*/
cityname: string;
/**
*
*/
adname: string;
/**
*
*/
email: string;
/**
*
*/
photos: PoiPhoto[];
/**
*
*/
entr_location: LngLat | null;
/**
*
*/
exit_location: LngLat | null;
/**
* @deprecated
*/
groupbuy: boolean;
/**
* @deprecated
*/
discount: boolean;
} & ({
indoor_map: true;
indoor_data: {
cpid: string;
floor: string;
truefloor: string;
};
} | {
indoor_map: false;
}) & {
/**
* @deprecated
*/
groupbuys?: Groupbuy[];
/**
* @deprecated
*/
discounts?: Discount[];
} & ({
deep_type: 'CINEMA';
/**
* @deprecated
*/
cinema: Cinema;
} | {
deep_type: 'DINING';
/**
* @deprecated
*/
dining: Dining;
} | {
deep_type: 'SCENIC';
/**
* @deprecated
*/
scenic: Scenic;
} | {
deep_type: 'HOTEL';
/**
* @deprecated
*/
hotel: Hotel;
});
interface Options {
/**
*
*/
city?: string;
/**
*
*/
citylimit?: boolean;
/**
* POI数据
* children=1POI数据children=0
*/
children?: number;
/**
* |
*/
type?: string;
/**
*
*/
lang?: Lang;
/**
*
*/
pageSize?: number;
/**
*
*/
pageIndex?: number;
/**
*
* base返回基本地址信息all返回基本+
*/
extensions?: 'base' | 'all';
/**
* Map对象
*/
map?: Map;
/**
* HTML容器id或容器元素
*/
panel?: string | HTMLElement;
/**
*
*/
showCover?: boolean;
/**
* UI风格
*/
renderStyle?: 'newpc' | 'default';
/**
* 使Marker点都处于视口的可见范围
*/
autoFitView?: boolean;
// internal
renderEngine?: string;
rankBy?: string;
}
interface PoiList {
/**
* Poi列表
*/
pois: Poi[]; // PlaceSearchPoiBase[] | PlaceSearchPoiExt[];
/**
*
*/
pageIndex: number;
/**
*
*/
pageSize: number;
/**
*
*/
count: number;
}
interface CityInfo {
/**
*
*/
name: string;
/**
*
*/
citycode: string;
/**
*
*/
adcode: string;
/**
*
*/
count: number;
}
interface SearchResult {
/**
*
*/
info: string;
/**
*
*/
poiList: PoiList;
/**
*
*/
keywordList?: string[];
/**
*
*/
cityList?: CityInfo[];
}
type Poi = PoiBase | PoiExt;
type SearchStatus = 'complete' | 'error' | 'no_data';
}
class PlaceSearch extends EventEmitter {
/**
*
* @param options
*/
constructor(options?: PlaceSearch.Options);
/**
*
* @param keyword
* @param callback
*/
search(
keyword: string,
callback: (status: PlaceSearch.SearchStatus, result: string | PlaceSearch.SearchResult) => void
): void;
/**
*
* @param keyword
* @param center
* @param radius
* @param callback
*/
searchNearBy(
keyword: string,
center: LocationValue,
radius: number,
callback: (status: PlaceSearch.SearchStatus, result: string | PlaceSearch.SearchResult) => void
): void;
/**
*
* @param keyword
* @param bounds
* @param callback
*/
searchInBounds(
keyword: string,
bounds: Bounds | Polygon,
callback: (status: PlaceSearch.SearchStatus, result: string | PlaceSearch.SearchResult) => void
): void;
/**
* POIID POI
* @param POIID POIID
* @param callback
*/
getDetails(
POIID: string,
callback: (status: PlaceSearch.SearchStatus, result: string | PlaceSearch.SearchResult) => void
): void;
/**
*
* @param type
*/
setType(type?: string): void;
/**
*
* @param limit
*/
setCityLimit(limit?: boolean): void;
/**
*
* @param pageIndex
*/
setPageIndex(pageIndex?: number): void;
/**
*
* @param pageSize
*/
setPageSize(pageSize?: number): void;
/**
*
* @param city
*/
setCity(city?: string): void;
/**
*
* @param lang
*/
setLang(lang?: Lang): void;
/**
*
*/
getLang(): Lang | undefined;
/**
*
*/
clear(): void;
/**
* marker页
* @param obj
*/
poiOnAMAP(obj: { location?: LocationValue; id: string; name?: string; }): void;
/**
* POI详情页
* @param obj
*/
detailOnAMAP(obj: { location?: LocationValue; id: string; name?: string; }): void;
// internal
open(): void;
close(): void;
}
}

View File

@ -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-place-search-tests.ts"
]
}

View File

@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}