English 中文(简体)
Untyped function calls may not accept type arguments.ts(2347)
原标题:

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.

问题回答

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.

  • The type argument it s referring to is the generic <...>.
  • The 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:

  • How you re importing Schema
  • What version of mongoose (and possibly @types/mongoose you re using)

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 ;




相关问题
Having many stacks with different types

I m making a C program that needs to use two stacks. One needs to hold chars, the other needs to hold doubles. I have two structs, node and stack: struct node { double value; struct node *...

Creating (boxed) primitive instance when the class is known

I need a method that returns an instance of the supplied class type. Let s assume that the supplied types are limited to such that an "empty" instance of them can be created. For instance, supplying ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

How to generate an instance of an unknown type at runtime?

i ve got the following in C#: string typename = "System.Int32"; string value = "4"; theses two strings should be taken to generate an object of the specified type with the specified value... result ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

热门标签