猫鼬精益对象类型不正确

发布于 2025-01-20 05:15:54 字数 1033 浏览 1 评论 0原文

我定义了一个数据模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晒暮凉 2025-01-27 05:15:54

挖掘文档后,我发现在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 to MapData.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文