打字稿编译器选择了错误的重载

Typescript compiler selecting wrong overload

mongoose.d.ts "DefinitelyTyped" 文件中,geoNear 函数有两个重载:

geoNear(point: { type: string; coordinates: number[] }, options: Object, callback?: (err: any, res: T[]) => void): Query<T[]>;
geoNear(point: number[], options: Object, callback?: (err: any, res: T[]) => void): Query<T[]>;

我将point定义为

const point = { type: "Point", coordinates: [lng, lat] }

其中 lnglat 都是 number,但是当我这样调用它时:

Location.geoNear(point, {
        spherical: true,
        maxDistance: theEarth.getRadiansFromDistance(maxDistance),
        num: 10
    }, (err, results, stats) => {
        var locations = []
        results.forEach((doc: any) => {
            locations.push({
                distance: theEarth.getDistanceFromRadians(doc.dis),
                name: doc.obj.name,
                address: doc.obj.address,
                rating: doc.obj.rating,
                facilities: doc.obj.facilities,
                _id: doc.obj._id
            })
        })
        res.status(200).json(locations)
    })

编译器抱怨

Argument of type '{ type: string; coordinates: number[]; }' is not assignable to parameter of type 'number[]'

如何强制 tsc 使用第一个重载?

您发送的回调需要 3 个参数,而不是接口中定义的 2 个。 您正在发送:

 }, (err, results, stats) => {

当方法期望时:

callback?: (err: any, res: T[]) => void

您的回电应该是:

}, (err, results) => {