Home > Back-end >  Compiling error sequelize-typescript but works with nodemon
Compiling error sequelize-typescript but works with nodemon

Time:10-13

I created a simple application using 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;
}

But when I execute this code with nodemon the script works and create the "User" table, but when I try to compile it using tsc it fails with this error :

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;

This is my 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"
  }
}

and my tsconfig.json

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

If anyone have an idea I take it, I don't understand how my code can be executed by nodemon but not compiled by tsc.

CodePudding user response:

Not sure what's causing this (maybe your Typescript version is incompatible with this sequelize version?) Perhaps adding skipLibCheck: true (so node_modules is not type-checked) to compilerOptions in tsconfig.json gets rid of the problem?

CodePudding user response:

Upgrade to sequelize 6.7.0 resolve this error.

  • Related