猫鼬精益对象类型不正确
我定义了一个数据模型:
type MapData = {
coordinates: [number, number][]; // coordinates consists of an array of [x,y] on the map
}
type MapDataDocument = MapData & mongoose.Document;
const mapDataSchema= new mongoose.Schema(
{coordinates: { type: [[Number, Number]], default: [] }}, { collection: 'mapData' });
export default mongoose.model<MapDataDocument>('mapData', mapDataSchema);
当我用lean获取它时:
const mapDataResults:MapDataDocument[] = mapData.find().lean(); //mapData is Model<MapDataDocument, {}, {}>
这将在构建时给出错误:
Argument of type 'Pick<_LeanDocument<MapDataDocument>, ... 14 more ' is not assignable to parameter of type 'MapDataDocument[]'.
Type 'number[]' is missing the following properties from type '[number, number]': 0, 1
我没有任何number[]的定义,我猜.lean
推断出类型错误?
我可能可以通过将坐标的类型定义更改为 number[][]
来解决这个问题,但我想保留 [number, number]
因为它涉及业务知识。
有什么方法可以解决打字问题?
I have a data model defined:
type MapData = {
coordinates: [number, number][]; // coordinates consists of an array of [x,y] on the map
}
type MapDataDocument = MapData & mongoose.Document;
const mapDataSchema= new mongoose.Schema(
{coordinates: { type: [[Number, Number]], default: [] }}, { collection: 'mapData' });
export default mongoose.model<MapDataDocument>('mapData', mapDataSchema);
When I am fetching it with lean:
const mapDataResults:MapDataDocument[] = mapData.find().lean(); //mapData is Model<MapDataDocument, {}, {}>
This will give an error on build as:
Argument of type 'Pick<_LeanDocument<MapDataDocument>, ... 14 more ' is not assignable to parameter of type 'MapDataDocument[]'.
Type 'number[]' is missing the following properties from type '[number, number]': 0, 1
I don't have any definition of number[], I guess the .lean
inferred the types incorrectly?
I can probably solve this problem by changing the coordinates' type definition to number[][]
but I want to keep [number, number]
as it pertains business knowledge.
What is the way to address the typing issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
挖掘文档后,我发现在
lean()
函数中,我还可以为其提供一种类型:lean&lt; mapdata&gt;()
,然后将更改返回的返回键入mapdata
。After digging into the documentation, I found that in
lean()
function, I can also provide a type to it:lean<MapData>()
Which will then change the returned type toMapData
.