【发布时间】: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