TypeScript newbie此处迁移现有项目。我们有猫鼬模型,类似于下面的代码片段,并使用discriminatorKey
属性:TypeScript:类型的参数不可分配
const task = new mongoose.Schema({
name: {
type: String
},
notes: {
type: String
}
}, {
discriminatorKey: 'type',
toObject: {
virtuals: true
},
toJSON: {
virtuals: true
}
});
这使我在编译过程中出现以下错误:
src/models/task.ts(12,3): error TS2345: Argument of type '{ discriminatorKey: string; }' is not assignable to parameter of type 'SchemaOptions'.
Object literal may only specify known properties, and 'discriminatorKey' does not exist in type 'SchemaOptions'.
我使用这些@types定义,这似乎是最近的一个:
"@types/mongoose": "^4.7.8"
我了解,该类型定义没有指定discriminatorKey
(这是obviou当看到node_modules/@types/mongoose/index.d.ts
时狡猾可见),但我不明白(a)为什么(忽略?不同的版本?其他原因?),(b)我如何绕过这个错误?
(c)奖金问题:@types
定义的版本策略对我而言仍不清楚。我认为,类型定义应该与实际库的版本匹配,但是,通常看起来没有匹配的版本 - 例如我们正在使用express
版本。 4.13.4,但没有匹配的@types/express
版本可用。在这种情况下遵循的最佳做法是什么?
文字受制于*额外的财产检查*。搜索本页上的术语:https://www.typescriptlang.org/docs/handbook/interfaces.html – cartant