我正在尝试为sequelize-typescript创建一个新方法来解决问题,并使用declaration merging保留代码OO。
不幸的是,当添加我的新方法定义(initializeModels)时,编译器开始向sequelize-typescript代码本身不存在的方法抛出错误,并且在我的方法实现中无法使用this
来识别属性。
在我的@ exttypes / sequelize / index.d.ts
declare module "sequelize/types" {
class Sequelize {
initializeModels():void;
}
}
我也确实尝试将其放入方法.ts实现文件中,我尝试“扩展”“ sequelize / types” Sequelize类(在方法.ts实现文件中)
src / initializers / sequelize(我的initializeModels
实现)
// [...]
import { Sequelize as SequelizeType } from "sequelize/types";
SequelizeType.prototype.initializeModels = function() {
// modelManager is into SequelizeType, but the intellisence only display what inside me class Sequelize definition.
for(const model of this.modelManager.models) {
// [...]
}
}
src / server.ts(我的主要设备)
import {Sequelize} from "./initializers/sequelize";
const sequelize = new Sequelize({dialect: "sqlite", database: "./database.sqlite"});
sequelize.addModels([__dirname + "/**/models/*.js"]);
// either initializeModels() doesn't exist (if I don't declare it from my @exttypes) or it is sync().
sequelize.initializeModels();
sequelize.sync().then(_ => { console.log("Database model updated"); });
tsconfig.json
{
"compilerOptions": {
// [...]
"typeRoots": [
"@exttypes"
]
},
"include": [
"src/**/*.ts"
],
}