我如何告诉打字稿 getAssociation 函数存在于 Sequelize 模型中?

How can I tell typescript that the getAssociation function exists in a Sequelize model?

我有以下代码:

interface TableBAttributes {
    TableBId: number;
}

interface TableAAttributes {
    TableAId: number;
    TableB: TableB;
}

class TableA extends Model<TableAAttributes> {}
TableA.init({
   TableAId: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true
   },
   TableB: {
      DataTypes.INTEGER,
      references: {
         model: TableB,
         key: 'tableB_id'
      },
      field: 'tableB_id'
   }
}, {
  sequelize,
  modelName: 'TableA',
  tableName: 'TableA'
});

TableA.belongsTo(TableB, {
   foreignKey: 'tableB_id',
   as: 'TableB'
});

class TableB extends Model<TableBAttributes> {};
TableB.init({
    TableBId: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        allowNull: false,
        autoIncrement: true
    }
}, {
   sequelize,
   modelName: 'TableB',
   tableName: 'TableB'
});

(async () => {
   const test = await TableA.findByPk(1);
   const relatedTableB = await test.getTableB();
   console.log(relatedTableB);
})();

如果我 运行 此代码,一切正常,并且我从查询中取回关联的 TableB。我唯一的问题是,打字稿抱怨 test.getTableB() 函数不存在,因为这个方法是由 Sequelize 创建的。

我如何告诉打字稿这个方法存在?

检查它是否适合你。

更多info

import {
  Model,
  HasOneGetAssociationMixin,
} from "sequelize";

interface TableBAttributes {
  TableBId: number;
}

interface TableAAttributes {
  TableAId: number;
}

class TableA extends Model<TableAAttributes> {
  public getTableB!: HasOneGetAssociationMixin<TableB>
}

class TableB extends Model<TableBAttributes> { }

TableA.belongsTo(TableB, {
  foreignKey: 'tableB_id',
  as: 'TableB'
});

(async () => {
  const test = await TableA.findByPk(1);
  const relatedTableB = await test.getTableB();
  console.log(relatedTableB);
})();