【问题标题】:Compiling error sequelize-typescript but works with nodemon编译错误 sequelize-typescript 但适用于 nodemon
【发布时间】:2023-04-28 15:44:01
【问题描述】:

我使用 Sequelize/typescript 创建了一个简单的应用程序

src/index.ts

import { Sequelize } from "sequelize-typescript"


const sequelize = new Sequelize({
    database: 'typescript',
    dialect: 'mysql',
    username: 'root',
    password: 'root',
    host: "localhost",
    models: [__dirname + '/**/*.model.ts'],
})

sequelize.sync({ force: true }).then(() => console.log("Database connected!"))

src/models/user.model.ts

import { Table, Column, Model } from 'sequelize-typescript';

@Table
export default class User extends Model {
    @Column
    name!: string;
}

但是当我使用 nodemon 执行此代码时,脚本可以工作并创建“用户”表,但是当我尝试使用 tsc 编译它时,它会失败并出现以下错误:

node_modules/sequelize-typescript/dist/model/model/model.d.ts:10:31 - error TS2417: Class static side 'typeof import("/home/maxou/Documents/Learn-Typescript/Back/node_modules/sequelize-typescript/dist/model/model/model").Model' incorrectly extends base class static side 'typeof import("/home/maxou/Documents/Learn-Typescript/Back/node_modules/sequelize/types/lib/model").Model'.
  The types returned by 'init(...)' are incompatible between these types.
    Type 'Model<any, any>' is not assignable to type 'MS'.
      'MS' could be instantiated with an arbitrary type which could be unrelated to 'Model<any, any>'.

10 export declare abstract class Model<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes> extends OriginModel<TModelAttributes, TCreationAttributes> {
                                 ~~~~~

node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.d.ts:12:5 - error TS2416: Property 'model' in type 'Sequelize' is not assignable to the same property in base type 'Sequelize'.
  Type '<TCreationAttributes, TModelAttributes>(model: string | ModelType<TCreationAttributes, TModelAttributes>) => ModelCtor<Model<any, any>>' is not assignable to type '(modelName: string) => ModelCtor<Model<any, any>>'.
    Type 'import("/home/maxou/Documents/Learn-Typescript/Back/node_modules/sequelize-typescript/dist/model/model/model").ModelCtor<import("/home/maxou/Documents/Learn-Typescript/Back/node_modules/sequelize-typescript/dist/model/model/model").Model<any, any>>' is not assignable to type 'import("/home/maxou/Documents/Learn-Typescript/Back/node_modules/sequelize/types/lib/model").ModelCtor<import("/home/maxou/Documents/Learn-Typescript/Back/node_modules/sequelize/types/lib/model").Model<any, any>>'.
      Type 'ModelCtor<Model<any, any>>' is not assignable to type 'typeof Model'.
        The types returned by 'init(...)' are incompatible between these types.
          Type 'Model<any, any>' is not assignable to type 'MS'.
            'MS' could be instantiated with an arbitrary type which could be unrelated to 'Model<any, any>'.

12     model<TCreationAttributes, TModelAttributes>(model: string | ModelType<TCreationAttributes, TModelAttributes>): ModelCtor;

这是我的 package.json

{
  "dependencies": {
    "mysql2": "^2.3.0",
    "nodemon": "^2.0.13",
    "sequelize": "^6.6.5",
    "sequelize-typescript": "^2.1.0",
    "ts-node": "^10.2.1",
    "typescript": "^4.4.3"
  },
  "scripts": {
    "dev": "nodemon src/index.ts",
    "build": "tsc"
  }
}

还有我的 tsconfig.json

{
    "compilerOptions": {
        "module": "CommonJS",
        "target": "ES6",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "outDir": "dist",
        "strict": true
    },
    "include": [
        "src/**/*"
    ]
}

如果有人知道我接受它,我不明白我的代码如何由 nodemon 执行但不能由 tsc 编译。

【问题讨论】:

    标签: node.js typescript sequelize.js sequelize-typescript


    【解决方案1】:

    不确定是什么原因造成的(也许您的 Typescript 版本与此 sequelize 版本不兼容?)也许在 tsconfig.json 中添加 skipLibCheck: true(所以 node_modules 未经过类型检查)到 compilerOptions 摆脱问题?

    【讨论】:

    • 好的,现在正在编译,但是当我使用 node 运行我的脚本时,我收到了来自 sequelize 的错误
    【解决方案2】:

    升级到 sequelize 6.7.0 解决这个错误。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center