首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译错误sequelize-typescript,但适用于nodemon

编译错误sequelize-typescript,但适用于nodemon
EN

Stack Overflow用户
提问于 2021-10-12 12:58:37
回答 2查看 575关注 0票数 0

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

src/index.ts

代码语言:javascript
复制
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/model/user.model.ts

代码语言:javascript
复制
import { Table, Column, Model } from 'sequelize-typescript';

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

但是,当我使用nodemon执行这段代码时,脚本可以工作并创建"User“表,但当我尝试使用tsc编译它时,它会失败,并显示以下错误:

代码语言:javascript
复制
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

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
{
    "compilerOptions": {
        "module": "CommonJS",
        "target": "ES6",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "outDir": "dist",
        "strict": true
    },
    "include": [
        "src/**/*"
    ]
}

如果有人有我的想法,我不明白为什么我的代码可以由nodemon执行,而不是由tsc编译。

EN

回答 2

Stack Overflow用户

发布于 2021-10-12 13:44:06

不确定是什么导致了这种情况(可能您的打字版本与此sequelize版本不兼容?)也许向tsconfig.json中的compilerOptions添加skipLibCheck: true (这样就不会对node_modules进行类型检查)就可以解决这个问题?

票数 1
EN

Stack Overflow用户

发布于 2021-10-12 20:06:46

升级到sequelize 6.7.0解决此错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69540992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档