【发布时间】:2021-12-29 08:39:18
【问题描述】:
有一个简单的 nestJS 服务,它返回一个 mongoDb 查询的结果。 如您所见,我尝试设置预期的结果类型,即文档数组。
class MyDataset {
group: string
location: string
type: string
reported: Date
}
async getDatasets(): Promise<Array<MyDataset>> {
const Collection = this.db.collection('collection')
const result = await Collection.find({}).toArray()
return result // <-- ts error
}
但我确实收到了 TS 错误
Type 'Document[]' is not assignable to type 'MyDataset[]'.
Type 'Document' is missing the following properties from type 'MyDataset': group, location, type, reported
我不明白,我做错了什么。有人能解释一下这个问题吗?
【问题讨论】:
标签: javascript typescript nestjs