Reputation: 21
I have a user schema with typescript, bellow is my interface
interface IUser{
name: string;
email: string;
password: string;
isAdmin: boolean;
}
And below is the user schema
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;
i get the typescript error: Untyped function calls may not accept type arguments on the user schema, in express and mongoose with editor visual studio.
Upvotes: 2
Views: 9667
Reputation: 11
I had the same issues, after reading Stanislas' solution it fixed my problem
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
interface ILink {
discord_id: String;
linking_code: String;
}
const LinkSchema = new Schema<ILink>({
discord_id: {
type: String,
required: true,
unique: [true, 'Discord ID name must be unique'],
},
linking_code: {
type: String,
required: true,
unique: true,
}
});
var Link = mongoose.model('Link', LinkSchema);
module.exports = Link;
I updated the imports to this
import {Schema, model} from 'mongoose';
Upvotes: 1
Reputation: 2020
I'm assuming you're getting the error on this line:
const UserSchema = new Schema<IUser>(
As it is saying Untyped function calls may not accept type arguments
.
type argument
it's referring to is the generic <...>
.function
it's referring to is Schema
, meaning it doesn't know what Schema
is (typed as any
).This leads me to believe that there is either something wrong with the way you're importing Schema
, or with the way the typings for mongoose
have been installed.
I would suggest reading mongoose - TypeScript Support.
If you cannot figure out what's going wrong, then it would help if you told us:
Schema
@types/mongoose
you're using)Upvotes: 2