是否有与TypeScript兼容的方式访问Sequelize关联的实体?

是否有与TypeScript兼容的方式访问Sequelize关联的实体?,typescript,model,associations,sequelize.js,Typescript,Model,Associations,Sequelize.js,我正在开发一个运行Sequelize的TypeScript应用程序。这是一个简单的示例模型: import * as Sequelize from 'sequelize'; export interface MyAttribute { id: number, descr: string } export interface MyInstance extends Sequelize.Instance<MyAttribute>, MyAttribute { } ex

我正在开发一个运行Sequelize的TypeScript应用程序。这是一个简单的示例模型:

import * as Sequelize from 'sequelize';

export interface MyAttribute {
    id: number,
    descr: string
}

export interface MyInstance extends Sequelize.Instance<MyAttribute>, MyAttribute { }

export interface MyModel extends Sequelize.Model<MyInstance, MyAttribute> { }

export default class MyManager {

    //[...]

    constructor() {

        this.model = this.sequelize.define<MyInstance, MyAttribute>("MyEntity", {
            id: {
                type: Sequelize.INTEGER,
                primaryKey: true,
                autoIncrement: true
            },
            descr: {
                type: Sequelize.INTEGER
            }
        });
    }

    public initRelations() {
        this.model.belongsTo(OtherModel.model);
        this.model.sync({force:true});
    }
}
import*作为“Sequelize”的Sequelize;
导出接口MyAttribute{
身份证号码:,
描述:字符串
}
导出接口MyInstance扩展Sequelize.Instance,MyAttribute{}
导出接口MyModel扩展了Sequelize.Model{}
导出默认类MyManager{
//[...]
构造函数(){
this.model=this.sequelize.define(“MyEntity”{
身份证:{
类型:Sequelize.INTEGER,
primaryKey:没错,
自动递增:真
},
描述:{
类型:Sequelize.INTEGER
}
});
}
公共关系(){
this.model.belongsTo(OtherModel.model);
this.model.sync({force:true});
}
}
现在我想通过MyInstance对象访问OtherModel的关联实体。我尝试了getOtherModels(),但这在TypeScript编译期间无效。在这个TypeScript环境中,是否有其他方法可以访问关联实体及其属性


感谢您的帮助。

Sequelize将在运行时向对象添加方法,如
getOtherModels
,因此您只需告诉Typescript,它们将在运行时存在于
MyModel
实例上,以便您可以使用它们:

export interface MyInstance extends Sequelize.Instance<MyAttribute>, MyAttribute { 
    getOtherModels(): Promise<OtherModel[]>
}
导出接口MyInstance扩展Sequelize.Instance,MyAttribute{
getOtherModels():承诺
}

谢谢您的帮助!我尝试了您的方法,在初始化与belongsTo()的关联时,在我的实例接口中添加了“getfines():Promise;”,同时为options对象提供了“as:'fine'”属性。所以在我看来,这绝对是对的。但是,我在运行时没有得到“getFinds()”方法(使用VS代码调试控制台观察)。我还有什么把戏要做吗?