diff --git a/README.md b/README.md index f719d4e..7437e4f 100644 --- a/README.md +++ b/README.md @@ -62,20 +62,17 @@ https://cdn.jsdelivr.net/npm/amap-react-components@0.0.1/dist/amap-react-compone ### Typescript -安装官方的 jsapi 的 ts 声明 +安装 jsapi 的 ts 声明 ```shell -npm install @amap/amap-jsapi-types +npm install amap-jsapi-v2-types -D ``` -由于官方 ts 声明存在声明错误、缺漏,以及缺少 AMapUI 相关接口声明等问题, -本项目在官方声明上对接口声明做了改动。 +由于官方 ts 声明存在声明错误、缺漏,以及缺少 AMapUI 相关接口声明,甚至官方文档存在错误等问题 -因此,需要将此[声明文件](./src/@types/AMap.d.ts) 复制进您的项目。 -当使用了本项目暂时不支持的组件,您需要自定义组件时,可以在您的项目自行维护。 +自定义组件时,可以参考[此文件](https://github.com/xyy94813/amap-react-components/blob/main/src/%40types/AMap.d.ts) 对其类型扩展 -> 如过,有人知道如何将这部分声明文件在构建时一同打包进最终的输出中,麻烦告知我。 -> 如果能看到您的 PR 那就更好了。 +并及时给 `amap-jsapi-v2-types` 提 PR。 ### Polyfill diff --git a/package-lock.json b/package-lock.json index 14c2b78..354f4ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,6 @@ "@amap/amap-jsapi-loader": "^1.0.1" }, "devDependencies": { - "@amap/amap-jsapi-types": "0.0.13", "@babel/core": "^7.23.0", "@babel/preset-env": "^7.23.3", "@babel/preset-react": "^7.23.3", @@ -29,6 +28,7 @@ "@types/geojson": "^7946.0.7", "@types/jest": "^26.0.3", "@types/react-dom": "^16.9.8", + "amap-jsapi-v2-types": "^1.0.0", "babel-loader": "^8.1.0", "dotenv-load": "^2.0.0", "eslint": "^7.32.0", @@ -77,12 +77,6 @@ "resolved": "https://registry.npmjs.org/@amap/amap-jsapi-loader/-/amap-jsapi-loader-1.0.1.tgz", "integrity": "sha512-nPyLKt7Ow/ThHLkSvn2etQlUzqxmTVgK7bIgwdBRTg2HK5668oN7xVxkaiRe3YZEzGzfV2XgH5Jmu2T73ljejw==" }, - "node_modules/@amap/amap-jsapi-types": { - "version": "0.0.13", - "resolved": "https://registry.npmjs.org/@amap/amap-jsapi-types/-/amap-jsapi-types-0.0.13.tgz", - "integrity": "sha512-hwp36URjQT9vDTmoUPYph3SEAiOvoUB+PGK0jZeZamgvaxew7rgc1XZWL/HphyMfRAqKwIYsAvXm9v8DTSjjzA==", - "dev": true - }, "node_modules/@ampproject/remapping": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.1.tgz", @@ -8599,6 +8593,12 @@ "ajv": ">=5.0.0" } }, + "node_modules/amap-jsapi-v2-types": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/amap-jsapi-v2-types/-/amap-jsapi-v2-types-1.0.0.tgz", + "integrity": "sha512-HEyQ9qerbiWrHsBSostannPQJ+2d93WaFXmQraXeWu2cFPzPP4J/oDx96Nk5sgsIFXn/FABS0qQMpBzeCZx8Iw==", + "dev": true + }, "node_modules/ansi-align": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/ansi-align/-/ansi-align-3.0.1.tgz", @@ -29307,12 +29307,6 @@ "resolved": "https://registry.npmjs.org/@amap/amap-jsapi-loader/-/amap-jsapi-loader-1.0.1.tgz", "integrity": "sha512-nPyLKt7Ow/ThHLkSvn2etQlUzqxmTVgK7bIgwdBRTg2HK5668oN7xVxkaiRe3YZEzGzfV2XgH5Jmu2T73ljejw==" }, - "@amap/amap-jsapi-types": { - "version": "0.0.13", - "resolved": "https://registry.npmjs.org/@amap/amap-jsapi-types/-/amap-jsapi-types-0.0.13.tgz", - "integrity": "sha512-hwp36URjQT9vDTmoUPYph3SEAiOvoUB+PGK0jZeZamgvaxew7rgc1XZWL/HphyMfRAqKwIYsAvXm9v8DTSjjzA==", - "dev": true - }, "@ampproject/remapping": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.1.tgz", @@ -35482,6 +35476,12 @@ "peer": true, "requires": {} }, + "amap-jsapi-v2-types": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/amap-jsapi-v2-types/-/amap-jsapi-v2-types-1.0.0.tgz", + "integrity": "sha512-HEyQ9qerbiWrHsBSostannPQJ+2d93WaFXmQraXeWu2cFPzPP4J/oDx96Nk5sgsIFXn/FABS0qQMpBzeCZx8Iw==", + "dev": true + }, "ansi-align": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/ansi-align/-/ansi-align-3.0.1.tgz", diff --git a/package.json b/package.json index 383bf17..f3376a5 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "react-dom": ">16.8" }, "devDependencies": { - "@amap/amap-jsapi-types": "0.0.13", "@babel/core": "^7.23.0", "@babel/preset-env": "^7.23.3", "@babel/preset-react": "^7.23.3", @@ -56,6 +55,7 @@ "@types/geojson": "^7946.0.7", "@types/jest": "^26.0.3", "@types/react-dom": "^16.9.8", + "amap-jsapi-v2-types": "^1.0.0", "babel-loader": "^8.1.0", "dotenv-load": "^2.0.0", "eslint": "^7.32.0", diff --git a/src/@types/AMap.d.ts b/src/@types/AMap.d.ts index f3d375c..8d24b71 100644 --- a/src/@types/AMap.d.ts +++ b/src/@types/AMap.d.ts @@ -1,122 +1,11 @@ /* eslint-disable import/no-extraneous-dependencies */ /* eslint-disable max-classes-per-file */ -import '@amap/amap-jsapi-types'; -// 对 amap type 做扩展 -declare global { - namespace AMap { - export interface ControlBarConfig extends AMap.ControlConfig { - showControlButton?: boolean; - } - export class ControlBar extends AMap.Control { - constructor(conf?: ControlBarConfig); - - _config: ControlBarConfig; - - _pitchUp: HTMLDivElement; - - _pitchDown: HTMLDivElement; - - _rotateLeft: HTMLDivElement; - - _rotateRight: HTMLDivElement; - - _compass: HTMLDivElement; - - _luopan: HTMLDivElement; - } - - export interface ToolBarConfig extends AMap.ControlConfig {} - - export class ToolBar extends AMap.Control { - constructor(conf?: ToolBarConfig); - } - - export interface ScaleConfig extends AMap.ControlConfig {} - - export class Scale extends AMap.Control { - constructor(conf?: ScaleConfig); - } - - export interface HawkEyeOptions { - autoMove?: boolean; // 是否随主图视口变化移动 - showRectangle?: boolean; // 是否显示视口矩形 - showButton?: boolean; // 是否显示打开关闭的按钮 - isOpen?: boolean; // 默认是否展开 - mapStyle?: string; // 缩略图要显示的地图自定义样式,如'amap://styles/dark' - // layers: array; // 缩略图要显示的图层类型,默认为普通矢量地图 - width?: string; // 缩略图的宽度,同CSS,如'200px' - height?: string; // 缩略图的高度,同CSS,如'200px' - offset?: [number, number]; // 缩略图距离地图右下角的像素距离,如 [2,2] - borderStyle?: string; // 缩略图的边框样式,同CSS,如"double solid solid double" - borderColor?: string; // 缩略图的高度,同CSS,如'silver' - borderRadius?: string; // 缩略图的高度,同CSS,如'5px' - borderWidth?: string; // 缩略图的高度,同CSS,如'2px' - buttonSize?: string; // 箭头按钮的像素尺寸,同CSS,如'12px' - } - - export class HawkEye extends AMap.Control { - constructor(options?: HawkEyeOptions); - open(): void; - close(): void; - } - - export interface GeoJSONGetOverlayCallBack { - ( - geojson?: GeoJSON.GeoJSON, - lnglat?: GeoJSON.Position[] | GeoJSON.Position[][] | GeoJSON.Position[][][] - ): any; - } - - export interface GeoJSONOptions { - geoJSON?: GeoJSON.GeoJSON | null; - getMarker?: GeoJSONGetOverlayCallBack; - getPolyline?: GeoJSONGetOverlayCallBack; - getPolygon?: GeoJSONGetOverlayCallBack; - } - - export class GeoJSON extends OverlayGroup { - constructor(options: GeoJSONOptions); - - importData: (data: GeoJSON.GeoJSON) => {}; - - toGeoJSON: () => GeoJSON.GeoJSON; - } - - export type MapTypeLayerInfo = { - id: string; - enable: string; - name: string; - type: 'base' | 'overlay'; - // layer: AMap.Layer; - layer: any; // AMap.Layer 未暴露出来; - show: boolean; - }; - - export type MapTypeOptions = { - defaultType?: number; - showTraffic?: boolean; - showRoad?: boolean; - }; - - export class MapType { - constructor(options?: MapTypeOptions); - - addLayer: (layerInfo: MapTypeLayerInfo) => void; - - removeLayer: (id: string) => void; - - addTo: (map: AMap.Map) => void; - - show: () => void; - - hide: () => void; - - remove: () => void; - } - - // export class PolylineEditor2 extends _PolylineEditor {} - } +// import '@amap/amap-jsapi-types'; +import 'amap-jsapi-v2-types'; +declare global { + namespace AMap {} + // 对 amap type 做扩展 namespace AMapUI { export type SimpleMarkerOptions = Omit & { iconTheme?: string; diff --git a/src/components/AMapGeoJSON/interface.ts b/src/components/AMapGeoJSON/interface.ts index 9771256..97f737a 100644 --- a/src/components/AMapGeoJSON/interface.ts +++ b/src/components/AMapGeoJSON/interface.ts @@ -11,5 +11,5 @@ export type AMapGeoJSONProps = AMap.GeoJSONOptions & { getPolygon?: AMapGeoJSONGetOverlayCallback; getPolyline?: AMapGeoJSONGetOverlayCallback; visible?: boolean; - options?: AMap.PolygonSharedOptions; + options?: Parameters[0]; }; diff --git a/src/components/AMapMap/AMapMap.tsx b/src/components/AMapMap/AMapMap.tsx index d59a0ab..d1bbec3 100644 --- a/src/components/AMapMap/AMapMap.tsx +++ b/src/components/AMapMap/AMapMap.tsx @@ -19,7 +19,7 @@ const CONTAINER_STYLE = { width: '100%', height: '100%' }; const defaultProps = { // eslint-disable-next-line react/default-props-match-prop-types - viewMode: '2D', + viewMode: '2D' as AMap.MapOptions['viewMode'], // eslint-disable-next-line react/default-props-match-prop-types features: ['bg', 'point', 'road', 'building'], }; @@ -58,7 +58,7 @@ const AMapMap = forwardRef>( if (p === 'add') { const newAddFunc: typeof target.add = (overlays) => { const result = target.add(overlays); - newInstance.emit('overlaysAdded' as AMap.EventType, overlays); + newInstance.emit('overlaysAdded', overlays); return result; }; return newAddFunc; @@ -67,7 +67,7 @@ const AMapMap = forwardRef>( if (p === 'remove') { const newRemoveFunc: typeof target.remove = (overlays) => { const result = target.remove(overlays); - newInstance.emit('overlaysRemoved' as AMap.EventType, overlays); + newInstance.emit('overlaysRemoved', overlays); return result; }; return newRemoveFunc; @@ -105,7 +105,7 @@ const AMapMap = forwardRef>( // set city useEffect(() => { if (cityName) { - curMap?.setCity(cityName, () => {}); + curMap?.setCity(cityName); } }, [cityName, curMap]); diff --git a/src/components/AMapMap/__tests__/AMapMap.test.tsx b/src/components/AMapMap/__tests__/AMapMap.test.tsx index 5506489..885d851 100644 --- a/src/components/AMapMap/__tests__/AMapMap.test.tsx +++ b/src/components/AMapMap/__tests__/AMapMap.test.tsx @@ -69,7 +69,7 @@ describe('AMapMap', () => { test('set city name', () => { render(); - expect(mockMapInstance.setCity).toBeCalledWith('深圳', expect.any(Function)); + expect(mockMapInstance.setCity).toBeCalledWith('深圳'); }); test('set zoom', () => { diff --git a/src/components/AMapMarker/__tests__/AMapMarker.test.tsx b/src/components/AMapMarker/__tests__/AMapMarker.test.tsx index 2ae1be0..35e4fca 100644 --- a/src/components/AMapMarker/__tests__/AMapMarker.test.tsx +++ b/src/components/AMapMarker/__tests__/AMapMarker.test.tsx @@ -75,7 +75,7 @@ describe('AMapMarker Component', () => { offset: [0, 10], direction: 'center', }; - render(); + render(); expect(mockInstance.setLabel).toHaveBeenCalledWith(label); }); diff --git a/src/components/AMapMarker/interface.ts b/src/components/AMapMarker/interface.ts index 51e7692..8ee02a1 100644 --- a/src/components/AMapMarker/interface.ts +++ b/src/components/AMapMarker/interface.ts @@ -1,27 +1,36 @@ export type AMapMarkerProps = AMap.MarkerOptions & { - position: AMap.Vector2; - anchor?: 'top-left' | 'top-center' | 'top-right' | 'middle-left' | 'center' | 'middle-right' | 'bottom-left' | 'bottom-center' | 'bottom-right'; + position: AMap.LngLatLike; + anchor?: + | 'top-left' + | 'top-center' + | 'top-right' + | 'middle-left' + | 'center' + | 'middle-right' + | 'bottom-left' + | 'bottom-center' + | 'bottom-right'; // visible?: boolean; // isTop?: boolean; // onHide?: (event?: any) => void; onShow?: (event?: any) => void; - onClick?: (event?: any) => void - onDBLClick?: (event?: any) => void - onRightClick?: (event?: any) => void - onMousemove?: (event?: any) => void - onMousedown?: (event?: any) => void - onMouseup?: (event?: any) => void - onMouseover?: (event?: any) => void - onMouseout?: (event?: any) => void - onTouchstart?: (event?: any) => void - onTouchmove?: (event?: any) => void - onTouchend?: (event?: any) => void - onDragstart?: (event?: any) => void - onDragging?: (event?: any) => void - onDragend?: (event?: any) => void - onMoving?: (event?: any) => void - onMoveEnd?: (event?: any) => void - onMoveAlong?: (event?: any) => void + onClick?: (event?: any) => void; + onDBLClick?: (event?: any) => void; + onRightClick?: (event?: any) => void; + onMousemove?: (event?: any) => void; + onMousedown?: (event?: any) => void; + onMouseup?: (event?: any) => void; + onMouseover?: (event?: any) => void; + onMouseout?: (event?: any) => void; + onTouchstart?: (event?: any) => void; + onTouchmove?: (event?: any) => void; + onTouchend?: (event?: any) => void; + onDragstart?: (event?: any) => void; + onDragging?: (event?: any) => void; + onDragend?: (event?: any) => void; + onMoving?: (event?: any) => void; + onMoveEnd?: (event?: any) => void; + onMoveAlong?: (event?: any) => void; }; diff --git a/src/components/AMapOverlayGroup/interface.ts b/src/components/AMapOverlayGroup/interface.ts index 16d61f3..35158ec 100644 --- a/src/components/AMapOverlayGroup/interface.ts +++ b/src/components/AMapOverlayGroup/interface.ts @@ -1,5 +1,5 @@ export type AMapOverlayGroupProps = { - options?: AMap.PolygonSharedOptions; + options?: Parameters[0]; visible?: Boolean; onShow?: (event?: any) => void; onHide?: (event?: any) => void; diff --git a/src/components/AMapPolygonEditor/AMapPolygonEditor.tsx b/src/components/AMapPolygonEditor/AMapPolygonEditor.tsx index 618cf18..9a98276 100644 --- a/src/components/AMapPolygonEditor/AMapPolygonEditor.tsx +++ b/src/components/AMapPolygonEditor/AMapPolygonEditor.tsx @@ -29,7 +29,8 @@ const AMapPolygonEditor: FC = ({ if (!map) return; if (!curInstance) return; const allPolygons: AMap.Polygon[] = map.getAllOverlays('polygon') || []; - const newTarget = computeTarget(allPolygons) ?? null; + // 为了通过 ts 检查统一为 undefined。实际运行 undefined 和 null 的行为一致 + const newTarget = computeTarget(allPolygons) ?? undefined; curInstance.setTarget(newTarget); }, [map, computeTarget, curInstance]); @@ -56,11 +57,11 @@ const AMapPolygonEditor: FC = ({ }; clearEffect = () => { - map.off('overlaysRemoved' as AMap.EventType, handleOverlayChanged); - map.off('overlaysAdded' as AMap.EventType, handleOverlayChanged); + map.off('overlaysRemoved', handleOverlayChanged); + map.off('overlaysAdded', handleOverlayChanged); }; - map.on('overlaysRemoved' as AMap.EventType, handleOverlayChanged); - map.on('overlaysAdded' as AMap.EventType, handleOverlayChanged); + map.on('overlaysRemoved', handleOverlayChanged); + map.on('overlaysAdded', handleOverlayChanged); return clearEffect; }, [map, reSetTarget]); @@ -100,11 +101,11 @@ const AMapPolygonEditor: FC = ({ }; clearEffect = () => { - map.off('overlaysRemoved' as AMap.EventType, handleOverlayChanged); - map.off('overlaysAdded' as AMap.EventType, handleOverlayChanged); + map.off('overlaysRemoved', handleOverlayChanged); + map.off('overlaysAdded', handleOverlayChanged); }; - map.on('overlaysRemoved' as AMap.EventType, handleOverlayChanged); - map.on('overlaysAdded' as AMap.EventType, handleOverlayChanged); + map.on('overlaysRemoved', handleOverlayChanged); + map.on('overlaysAdded', handleOverlayChanged); return clearEffect; }, [map, reSetAdsorbPolygons]); diff --git a/src/components/AMapPolygonEditor/__tests__/AMapPolygonEditor.test.tsx b/src/components/AMapPolygonEditor/__tests__/AMapPolygonEditor.test.tsx index cec0b12..fd7da14 100644 --- a/src/components/AMapPolygonEditor/__tests__/AMapPolygonEditor.test.tsx +++ b/src/components/AMapPolygonEditor/__tests__/AMapPolygonEditor.test.tsx @@ -91,7 +91,7 @@ describe('AMapPolygonEditor', () => { expect(mockMapInstance.getAllOverlays).toBeCalledWith('polygon'); expect(computeTarget).toBeCalledWith([]); expect(computeAdsorbPolygons).toBeCalledWith([]); - expect(mockInstance.setTarget).toBeCalledWith(null); + expect(mockInstance.setTarget).toBeCalledWith(undefined); }); test('sets target correctly when map.getAllOverlays returns null', () => { @@ -105,7 +105,7 @@ describe('AMapPolygonEditor', () => { expect(mockMapInstance.getAllOverlays).toBeCalledWith('polygon'); expect(computeTarget).toBeCalledWith([]); expect(computeAdsorbPolygons).toBeCalledWith([]); - expect(mockInstance.setTarget).toBeCalledWith(null); + expect(mockInstance.setTarget).toBeCalledWith(undefined); }); test('triggers onChange correctly', () => { diff --git a/src/components/AMapPolylineEditor/AMapPolylineEditor.tsx b/src/components/AMapPolylineEditor/AMapPolylineEditor.tsx index 6903c1b..eba3267 100644 --- a/src/components/AMapPolylineEditor/AMapPolylineEditor.tsx +++ b/src/components/AMapPolylineEditor/AMapPolylineEditor.tsx @@ -4,9 +4,6 @@ import { useEffect, useRef, } from 'react'; -import type { - _PolylineEditor as PolylineEditor, -} from '@amap/amap-jsapi-types/plugins/PolylineEditor'; import useAMap from '../../hooks/useAMap'; import useAMapPluginInstance from '../../hooks/useAMapPluginInstance'; @@ -25,7 +22,7 @@ const AMapPolylineEditor: FC = ({ const initInstance = useCallback((AMap, map) => new AMap!.PolylineEditor(map), []); // why PolylineEditor2, see: https://github.com/AMap-Web/amap-jsapi-types/pull/22 - const curInstance = useAMapPluginInstance('PolylineEditor', initInstance); + const curInstance = useAMapPluginInstance('PolylineEditor', initInstance); const { map } = useAMap(); const reSetTarget = useCallback(() => { @@ -59,11 +56,11 @@ const AMapPolylineEditor: FC = ({ }; clearEffect = () => { - map.off('overlaysRemoved' as AMap.EventType, handleOverlayChanged); - map.off('overlaysAdded' as AMap.EventType, handleOverlayChanged); + map.off('overlaysRemoved', handleOverlayChanged); + map.off('overlaysAdded', handleOverlayChanged); }; - map.on('overlaysRemoved' as AMap.EventType, handleOverlayChanged); - map.on('overlaysAdded' as AMap.EventType, handleOverlayChanged); + map.on('overlaysRemoved', handleOverlayChanged); + map.on('overlaysAdded', handleOverlayChanged); return clearEffect; }, [map, reSetTarget]); diff --git a/src/helpers/__tests__/amapPolygonPath2GeoJSONCoords.test.ts b/src/helpers/__tests__/amapPolygonPath2GeoJSONCoords.test.ts index f7f2365..cf8842b 100644 --- a/src/helpers/__tests__/amapPolygonPath2GeoJSONCoords.test.ts +++ b/src/helpers/__tests__/amapPolygonPath2GeoJSONCoords.test.ts @@ -2,18 +2,50 @@ import amapPolygonPath2GeoJSONCoords from '../amapPolygonPath2GeoJSONCoords'; // mock LngLat class class LngLat { + public className: string; + public lat: number; public lng: number; + pos: [number, number]; + constructor(lng: number, lat: number) { this.lat = lat; this.lng = lng; } - public toArray() { + public toArray(): [number, number] { return [this.lng, this.lat]; } + + setLng = jest.fn(); + + setLat = jest.fn(); + + getLng = jest.fn(); + + getLat = jest.fn(); + + equals = jest.fn(); + + add = jest.fn(); + + subtract = jest.fn(); + + divideBy = jest.fn(); + + multiplyBy = jest.fn(); + + offset = jest.fn(); + + toString = jest.fn(); + + toJSON = jest.fn(); + + distance = jest.fn(); + + distanceTo = jest.fn(); } // mock AMap api @@ -23,12 +55,12 @@ const AMap = { describe('amapPolygonPath2GeoJSONCoords', () => { test('should convert AMap polygon path to GeoJSON coordinates', () => { - const amapPath: ReturnType = [ + const amapPath: AMap.LngLat[] = [ new AMap.LngLat(1, 2), new AMap.LngLat(3, 4), new AMap.LngLat(5, 6), ]; - const expectedCoords: typeof amapPath = [ + const expectedCoords: [number, number][] = [ [1, 2], [3, 4], [5, 6], @@ -39,7 +71,7 @@ describe('amapPolygonPath2GeoJSONCoords', () => { }); test('should convert correctly if it is a Polygon that has a hole', () => { - const amapPolygonWithHolePath: ReturnType[] = [ + const amapPolygonWithHolePath: AMap.LngLat[][] = [ // outer-line [ new AMap.LngLat(0, 0), @@ -53,7 +85,7 @@ describe('amapPolygonPath2GeoJSONCoords', () => { new AMap.LngLat(2, 1), ], ]; - const expectedCoords: typeof amapPolygonWithHolePath = [ + const expectedCoords: [number, number][][] = [ [ [0, 0], [0, 10], @@ -72,7 +104,7 @@ describe('amapPolygonPath2GeoJSONCoords', () => { }); test('should convert correctly if it is a MultiPolygon that has a hole', () => { - const amapMultiPolygonPath: ReturnType[] = [ + const amapMultiPolygonPath: AMap.LngLat[][][] = [ // polygon 1 [ [ @@ -101,7 +133,7 @@ describe('amapPolygonPath2GeoJSONCoords', () => { ], ]; - const expectedCoords: typeof amapMultiPolygonPath = [ + const expectedCoords: [number, number][][][] = [ // polygon 1 [ [ @@ -132,6 +164,6 @@ describe('amapPolygonPath2GeoJSONCoords', () => { }); test('should throw an error if the input is not an array', () => { - expect(() => amapPolygonPath2GeoJSONCoords(undefined)).toThrowError('invalid path'); + expect(() => amapPolygonPath2GeoJSONCoords(undefined as any)).toThrowError('invalid path'); }); }); diff --git a/src/helpers/amapPolygonPath2GeoJSONCoords.ts b/src/helpers/amapPolygonPath2GeoJSONCoords.ts index 7b8e608..5ff02d9 100644 --- a/src/helpers/amapPolygonPath2GeoJSONCoords.ts +++ b/src/helpers/amapPolygonPath2GeoJSONCoords.ts @@ -1,16 +1,14 @@ import { isLatLngArr } from './amapHelper'; /** -- * 补充最后一个 Position。 -- * 支持转换至 GeoJSON.Polygon 和 GeoJSON.MultiplePolygon 的 coords -+ * 将 AMap 多边形路径转换为 GeoJSON 坐标。 -+ * -+ * @param path - 要转换的 AMap.Polygon 路径。 -+ * @returns 转换后的 GeoJSON 坐标。 + * 补充最后一个 Position。 + * 支持转换至 GeoJSON.Polygon 和 GeoJSON.MultiplePolygon 的 coords + * 将 AMap 多边形路径转换为 GeoJSON 坐标。 + * + * @param path - 要转换的 AMap.Polygon 路径。 + * @returns 转换后的 GeoJSON 坐标。 */ -const amapPolygonPath2GeoJSONCoords = ( - path: ReturnType, -): typeof path => { +const amapPolygonPath2GeoJSONCoords = (path: ReturnType): any => { if (!Array.isArray(path)) throw Error('invalid path'); // 检查路径是否是一个 LatLng 数组,并且至少有一个元素 @@ -23,7 +21,7 @@ const amapPolygonPath2GeoJSONCoords = ( } // 递归地将每个嵌套的路径转换为 GeoJSON 坐标 - return (path as AMap.LngLat[][][])!.map(amapPolygonPath2GeoJSONCoords); + return (path as any)!.map(amapPolygonPath2GeoJSONCoords); }; export default amapPolygonPath2GeoJSONCoords; diff --git a/src/hooks/useControlContainerCSSText.ts b/src/hooks/useControlContainerCSSText.ts index 042463e..eebf682 100644 --- a/src/hooks/useControlContainerCSSText.ts +++ b/src/hooks/useControlContainerCSSText.ts @@ -1,7 +1,7 @@ const formatPX = (val: number | string) => `${encodeURIComponent(val)}px`; const useControlContainerCSSText = ( - position: AMap.ControlConfig['position'], + position: AMap.CommonControlConfig['position'], offset = [10, 10], ) => { if (position === 'LT') { diff --git a/src/hooks/useControlContainerUpdater.ts b/src/hooks/useControlContainerUpdater.ts index a084da6..853a3a1 100644 --- a/src/hooks/useControlContainerUpdater.ts +++ b/src/hooks/useControlContainerUpdater.ts @@ -5,7 +5,7 @@ import useControlContainerCSSText from './useControlContainerCSSText'; const useControlContainerUpdater = ( instance: T, - position: AMap.ControlConfig['position'], + position: AMap.CommonControlConfig['position'], offset = [10, 10], ) => { const containerCSSText = useControlContainerCSSText(position, offset);