首页 > 解决方案 > 对象字面量只能指定已知的属性,而 'id' 不存在于类型 'ModelAttributes' 中

问题描述

我正在尝试使用 sequelize ORM 定义数据库用户模型,但我正在努力解决我需要帮助解决的 typescript 编译器引发的类型错误。我需要解决的代码。

export interface IUserAddModel{
   email: string,
    password: string
 }

 export interface IUserModel extends Sequelize.Model<IUserModel,IUserAddModel>{
   id: number,
   email: string
   password: string
   createdAt?: string
   updatedAt?: string
}

export interface IUserViewModel{
    id: number,
    email: string
}


//Define a new model, representing a table in the DB.
export const User = sequelizeObj.define<IUserModel,IUserAddModel>('user', {
   id:{ // This id property is bringing about the type issue.. yet I defined in the 
        //IUserModel interface. Do not know why it is bring compiler error issues
      type: Sequelize.INTEGER,
      autoIncrement: true,
      primaryKey:true
   },
   email: Sequelize.STRING,
   password: Sequelize.STRING 
})

错误

Object literal may only specify known properties, and 'id' does not exist in type 'ModelAttributes<IUserModel, IUserAddModel>'

但是当我从定义的模型中删除 id 时,打字稿编译器停止抱怨,但我需要数据库表中的 id。

标签: typescriptsequelize.js

解决方案


推荐阅读