我有一个带有 typescript 的用户架构,下面是我的界面
interface IUser{
name: string;
email: string;
password: string;
isAdmin: boolean;
}
下面是用户架构
const UserSchema = new Schema<IUser>(
{
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
validate: [validator.isEmail, "Please provide a valid email"],
},
password: {
type: String,
required: true,
minlength: 8,
},
isAdmin: {
type: Boolean,
required: true,
default: false,
},
},
{
timestamps: true,
}
);
const UserModel = model("User", UserSchema);
module.exports = UserModel;
我收到 typescript 错误:在使用 Visual Studio 编辑器的 Express 和 mongoose 中,非类型化函数调用可能不接受用户架构上的类型参数。
最佳答案
我假设您在这一行遇到错误:
const UserSchema = new Schema<IUser>(
正如它所说的Untyped function calls may not accept type arguments
.
type argument
它指的是通用<...>
.function
它指的是Schema
,这意味着它不知道什么Schema
是(输入为any
)。
这让我相信您的导入方式有问题 Schema
,或者输入 mongoose
的方式已安装。
我建议阅读mongoose - TypeScript Support .
如果您无法弄清楚出了什么问题,那么告诉我们将会有所帮助:
- 您如何导入
Schema
- 您使用的 mongoose 版本(可能还有
@types/mongoose
)
关于typescript - 无类型函数调用可能不接受类型参数。ts(2347),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70261102/