编译错误序列类型脚本,但与nodemon一起工作



我使用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/模型/user.model.ts

import { Table, Column, Model } from 'sequelize-typescript';
@Table
export default class User extends Model {
@Column
name!: string;
}

但是当我使用nodemon执行这段代码时,脚本工作并创建了"User"表,但当我尝试使用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;

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"
}
}

和我的tsconfig.json

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

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

不确定是什么原因造成的(也许你的Typescript版本与这个sequelize版本不兼容?)也许在tsconfig.json中添加skipLibCheck: true(因此node_modules没有类型检查)到compilerOptions可以解决问题?

升级到序列化6.7.0解决此错误。

相关内容

最新更新