如何绕过 ESLint 调用 Typescript 中未定义的 Geolocation 接口?

How can I bypass ESLint calling the Geolocation interface undefined in Typescript?

我正在使用 Geolocation API 在地图上放置标记,但 ESLint 抱怨 GeolocationPosition 未定义。尽管如此,代码运行并给出了预期的结果,但我想清理我的代码以获取警告。我是打字稿和 ESlint 的新手,所以我不确定从这里去哪里。有什么建议么?代码和警告图像如下。

import { Map } from 'leaflet';
import { iconLocation, setMarker } from './leafletMarker';

export function getUserPosition(callback : (position : GeolocationPosition) => void) {
    function success(position : GeolocationPosition) {
        callback(position);
    }
    function error() {
        console.log('Unable to retrieve your location'); // change to alerts when Alert component is ready
    }
    if (!navigator.geolocation) {
        console.log('Geolocation is not supported by your browser'); // change to alerts when Alert component is ready
    } else {
        navigator.geolocation.getCurrentPosition(success, error, { enableHighAccuracy: true });
    }
}

export function handleMyPosition(map: Map) {
    getUserPosition((userPosition: GeolocationPosition) => {
        if (map) {
            const { latitude, longitude } = userPosition.coords;
            setMarker(map, latitude, longitude, { icon: iconLocation });
            map?.setView([latitude, longitude], 9);
        }
    });
}

Don't use no-undef in TypeScript projects。只需在您的配置中禁用该规则,TypeScript 的功能相同但更好。