Node.js 类型脚本编译器选择了错误的重载
在Node.js 类型脚本编译器选择了错误的重载,node.js,mongoose,typescript,overloading,Node.js,Mongoose,Typescript,Overloading,在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
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[]>;
其中lng
和lat
都是number
s,但是当我用这个调用它时:
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使用第一个重载?您发送的calback需要3个参数,而不是界面中定义的2个参数。 您正在发送:
}, (err, results, stats) => {
当方法期望:
callback?: (err: any, res: T[]) => void
您的回拨应该是:
}, (err, results) => {
似乎DefiniteTyped处的
.d.ts
文件对此函数不正确。回调参数应该是(err:any,result:T[],stats:any)=>void
,但您的答案是正确的。
}, (err, results) => {