【发布时间】:2019-12-16 23:24:05
【问题描述】:
我正在将我的项目移植到 typescript,但我的架构遇到了一个名为 alias 的字段。我收到此错误:
Types of property 'alias' are incompatible.
Type 'StringConstructor' is not assignable to type 'string'.ts(2345)
我知道这是因为 mongoose 有别名字段 (https://mongoosejs.com/docs/guide.html#aliases)。我的问题是,我该如何逃避呢?
我的代码供参考:
import mongoose, { Schema, Model } from 'mongoose';
const userSchema = new Schema({
_id: Schema.Types.ObjectId,
alias: String
});
谢谢。
编辑:这也不起作用:
import mongoose, { Schema, Model } from 'mongoose';
const userSchema = new Schema({
_id: Schema.Types.ObjectId,
alias: {type: String, required: true}
});
出现此错误:
Types of property 'alias' are incompatible.
Type '{ type: string; required: boolean; }' is not assignable to type 'string'.ts(2345)
【问题讨论】:
标签: node.js typescript mongoose