Skip to content

Commit

Permalink
fix: 调整类型声明,解决 TS 报错
Browse files Browse the repository at this point in the history
  • Loading branch information
xyy94813 committed Oct 23, 2024
1 parent 95c9fb9 commit 139db84
Show file tree
Hide file tree
Showing 18 changed files with 42 additions and 28 deletions.
6 changes: 4 additions & 2 deletions src/components/AMapAPIContainer/createAMapAPIContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FC } from 'react';
import type { FC, ReactNode } from 'react';
import React, { lazy } from 'react';
import AMapLoader from '@amap/amap-jsapi-loader';

Expand Down Expand Up @@ -66,7 +66,9 @@ export const createAMapAPIContainer = (options: CreateAMapApiContainerOptions) =
__AMAP_UI__: AMapUI,
};

const AMapAPIContainer: FC = ({ children }) => (
const AMapAPIContainer: FC<{
children: ReactNode
}> = ({ children }) => (
<AMapAPIContext.Provider value={APICtx}>
{children}
</AMapAPIContext.Provider>
Expand Down
2 changes: 1 addition & 1 deletion src/components/AMapCircle/AMapCircle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const AMapCircle = forwardRef<AMap.Circle, AMapCircleProps>(
draggable,
})
.filter(([,val]) => val !== undefined && val !== null)
.reduce((finallyObj, [key, val]) => {
.reduce((finallyObj: Record<string, any>, [key, val]) => {
// eslint-disable-next-line no-param-reassign
finallyObj[key] = val;
return finallyObj;
Expand Down
2 changes: 1 addition & 1 deletion src/components/AMapControlBar/AMapControlBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const AMapControlBar: FC<AMapControlBarProps> = ({
onHide,
onShow,
}) => {
const initInstance = useCallback((AMap) => new AMap!.ControlBar(), []);
const initInstance = useCallback((AMap: typeof global.AMap) => new AMap!.ControlBar(), []);
const curInstance = useAMapPluginInstance<AMap.ControlBar>('ControlBar', initInstance);

// 避免重新创建 ControlBar 实例,自行修改 dom 节点
Expand Down
4 changes: 2 additions & 2 deletions src/components/AMapEllipse/AMapEllipse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const AMapEllipse: FC<AMapEllipseProps> = ({
onTouchmove,
onTouchend,
}) => {
const initInstance = useCallback((AMap) => new AMap!.Ellipse(), []);
const initInstance = useCallback((AMap: typeof global.AMap) => new AMap!.Ellipse(), []);
const curInstance = useAMapPluginInstance<AMap.Ellipse>('Ellipse', initInstance);

useSetter<Parameters<AMap.Ellipse['setCenter']>>(curInstance, 'setCenter', center!);
Expand All @@ -80,7 +80,7 @@ const AMapEllipse: FC<AMapEllipseProps> = ({
draggable,
})
.filter(([,val]) => val !== undefined && val !== null)
.reduce((finallyObj, [key, val]) => {
.reduce((finallyObj: Record<string, any>, [key, val]) => {
// eslint-disable-next-line no-param-reassign
finallyObj[key] = val;
return finallyObj;
Expand Down
2 changes: 1 addition & 1 deletion src/components/AMapGeoJSON/AMapGeoJSON.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const AMapGeoJSON: FC<AMapGeoJSONProps> = ({
visible,
options,
}) => {
const initInstance = useCallback((AMap) => new AMap!.GeoJSON({}), []);
const initInstance = useCallback((AMap: typeof global.AMap) => new AMap!.GeoJSON({}), []);
const curInstance = useAMapPluginInstance<AMap.GeoJSON>('GeoJSON', initInstance);
const withMap = useWithAMap();

Expand Down
15 changes: 9 additions & 6 deletions src/components/AMapHawkEye/AMapHawkEye.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-disable react/default-props-match-prop-types */
import type { FC } from 'react';
import {
useEffect,
useMemo,
useCallback,
memo,
useEffect, useMemo, useCallback, memo,
} from 'react';

import useAMapPluginInstance from '../../hooks/useAMapPluginInstance';
Expand Down Expand Up @@ -77,8 +74,14 @@ const AMapHawkEye: FC<AMapHawkEyeProps> = ({
buttonSize,
]);

const initInstance = useCallback((AMap) => new AMap!.HawkEye(initConfig), [initConfig]);
const curInstance = useAMapPluginInstance<AMap.HawkEye>('HawkEye', initInstance);
const initInstance = useCallback(
(AMap: typeof global.AMap) => new AMap!.HawkEye(initConfig),
[initConfig],
);
const curInstance = useAMapPluginInstance<AMap.HawkEye>(
'HawkEye',
initInstance,
);

// 是否展开
useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/AMapMap/AMapMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const AMapMap = forwardRef<any, PropsWithChildren<AMapMapProps>>(
};
return newRemoveFunc;
}
return target[p];
return (target as any)[p];
},
});

Expand Down
2 changes: 1 addition & 1 deletion src/components/AMapMapType/AMapMapType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const AMapMapType: FC<AMapMapTypeProps> = ({
onHide,
onShow,
}) => {
const initInstance = useCallback((AMap) => new AMap!.MapType({
const initInstance = useCallback((AMap: typeof global.AMap) => new AMap!.MapType({
defaultType,
showTraffic,
showRoad,
Expand Down
5 changes: 4 additions & 1 deletion src/components/AMapMarker/AMapMarker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ const AMapMarker: FC<AMapMarkerProps> = ({
onMoveEnd,
onMoveAlong,
}) => {
const initInstance = useCallback((AMap) => new AMap!.Marker({ zooms }), [zooms]);
const initInstance = useCallback(
(AMap: typeof global.AMap) => new AMap!.Marker({ zooms }),
[zooms],
);
const curInstance = useAMapPluginInstance<AMap.Marker>('Marker', initInstance);

useVisible(curInstance, !!visible);
Expand Down
2 changes: 1 addition & 1 deletion src/components/AMapOverlayGroup/AMapOverlayGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const AMapOverlayGroup = forwardRef<AMap.OverlayGroup, PropsWithChildren<AMapOve
},
ref,
) => {
const initInstance = useCallback((AMap) => new AMap!.OverlayGroup(), []);
const initInstance = useCallback((AMap: typeof global.AMap) => new AMap!.OverlayGroup(), []);
const curInstance = useAMapPluginInstance<AMap.OverlayGroup>('OverlayGroup', initInstance);

useImperativeHandle(ref, () => curInstance!, [curInstance]);
Expand Down
4 changes: 2 additions & 2 deletions src/components/AMapPolygon/AMapPolygon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const AMapPolygon = forwardRef<AMap.Polygon, AMapPolygonProps>(({
onTouchmove,
onTouchend,
}, ref) => {
const initInstance = useCallback((AMap) => new AMap!.Polygon(), []);
const initInstance = useCallback((AMap: typeof global.AMap) => new AMap!.Polygon(), []);
const curInstance = useAMapPluginInstance<AMap.Polygon>('Polygon', initInstance);

useImperativeHandle(ref, () => curInstance!, [curInstance]);
Expand All @@ -87,7 +87,7 @@ const AMapPolygon = forwardRef<AMap.Polygon, AMapPolygonProps>(({
wallColor,
})
.filter(([,val]) => val !== undefined && val !== null)
.reduce((finallyObj, [key, val]) => {
.reduce((finallyObj: Record<string, any>, [key, val]) => {
// eslint-disable-next-line no-param-reassign
finallyObj[key] = val;
return finallyObj;
Expand Down
5 changes: 4 additions & 1 deletion src/components/AMapPolygonEditor/AMapPolygonEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ const AMapPolygonEditor: FC<AMapPolygonEditorProps> = ({
const $lastOnChange = useRef<AMapPolygonEditorProps['onChange']>(onChange);
$lastOnChange.current = onChange;

const initInstance = useCallback((AMap, map) => new AMap!.PolygonEditor(map), []);
const initInstance = useCallback(
(AMap: typeof global.AMap, map: AMap.Map) => new AMap!.PolygonEditor(map),
[],
);
const curInstance = useAMapPluginInstance<AMap.PolygonEditor>('PolygonEditor', initInstance);
const { map } = useAMap();

Expand Down
4 changes: 2 additions & 2 deletions src/components/AMapPolyline/AMapPolyline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const AMapPolyline = forwardRef<AMap.Polyline, AMapPolylineProps>(({
onTouchmove,
onTouchend,
}, ref) => {
const initInstance = useCallback((AMap) => new AMap!.Polyline(), []);
const initInstance = useCallback((AMap: typeof global.AMap) => new AMap!.Polyline(), []);
const curInstance = useAMapPluginInstance<AMap.Polyline>('Polyline', initInstance);

useImperativeHandle(ref, () => curInstance!, [curInstance]);
Expand Down Expand Up @@ -98,7 +98,7 @@ const AMapPolyline = forwardRef<AMap.Polyline, AMapPolylineProps>(({
zooms,
})
.filter(([,val]) => val !== undefined && val !== null)
.reduce((finallyObj, [key, val]) => {
.reduce((finallyObj: Record<string, any>, [key, val]) => {
// eslint-disable-next-line no-param-reassign
finallyObj[key] = val;
return finallyObj;
Expand Down
5 changes: 4 additions & 1 deletion src/components/AMapPolylineEditor/AMapPolylineEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const AMapPolylineEditor: FC<AMapPolylineEditorProps> = ({
const $lastOnChange = useRef<AMapPolylineEditorProps['onChange']>(onChange);
$lastOnChange.current = onChange;

const initInstance = useCallback((AMap, map) => new AMap!.PolylineEditor(map), []);
const initInstance = useCallback(
(AMap: typeof global.AMap, map: AMap.Map) => new AMap!.PolylineEditor(map),
[],
);
// why PolylineEditor2, see: https://github.com/AMap-Web/amap-jsapi-types/pull/22
const curInstance = useAMapPluginInstance<AMap.PolylineEditor>('PolylineEditor', initInstance);
const { map } = useAMap();
Expand Down
4 changes: 2 additions & 2 deletions src/components/AMapRectangle/AMapRectangle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const AMapRectangle = forwardRef<AMap.Rectangle, AMapRectangleProps>(({
onTouchmove,
onTouchend,
}, ref) => {
const initInstance = useCallback((AMap) => new AMap!.Rectangle(), []);
const initInstance = useCallback((AMap: typeof global.AMap) => new AMap!.Rectangle(), []);
const curInstance = useAMapPluginInstance<AMap.Rectangle>(
'Rectangle',
initInstance,
Expand Down Expand Up @@ -86,7 +86,7 @@ const AMapRectangle = forwardRef<AMap.Rectangle, AMapRectangleProps>(({
bubble,
})
.filter(([, val]) => val !== undefined && val !== null)
.reduce((finallyObj, [key, val]) => {
.reduce((finallyObj: Record<string, any>, [key, val]) => {
// eslint-disable-next-line no-param-reassign
finallyObj[key] = val;
return finallyObj;
Expand Down
2 changes: 1 addition & 1 deletion src/components/AMapToolBar/AMapToolBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const defaultProps = {
const AMapToolBar: FC<AMapToolBarProps> = ({
position, offset, visible, onShow, onHide,
}) => {
const initInstance = useCallback((AMap) => new AMap.ToolBar(), []);
const initInstance = useCallback((AMap: typeof global.AMap) => new AMap.ToolBar(), []);
const curInstance = useAMapPluginInstance<AMap.ToolBar>('ToolBar', initInstance);

// 自行控制 Control container dom
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useAMapPluginInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const useAMapPluginInstance = <T = any>(
setInstance(instance);
};

if (AMap[pluginName]) {
if ((AMap as any)[pluginName]) {
init();
} else {
AMap.plugin([`AMap.${pluginName}`], init);
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useControlContainerCSSText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const useControlContainerCSSText = (
if (typeof position === 'object') {
// 保证了 position 中的 key 顺序不相同也有同样的结果,避免重复更新
return ['top', 'right', 'bottom', 'left'].reduce((result, key) => {
const value = position[key];
const value = (position as Record<string, any>)[key];
if (!value) return result;
return `${result}${key}:${formatPX(value!)};`;
}, '');
Expand Down

0 comments on commit 139db84

Please sign in to comment.