node.js - 'typeof Sequelize' 类型缺少 'Sequelize' : Sequelize, 配置、modelManager、connectionManager 等 23 个类型中的以下属性

标签 node.js typescript sequelize.js

我正在尝试将 sequelize v5 与 TypeScript 一起使用。使用 commonjs 时,sequelize 组织模型如下:
models/index.js :

var Sequelize = require('sequelize');
var config    = require(__dirname + '/../config/config.js')[env];
var db        = {};

if (config.use_env_variable) {
  var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  var sequelize = new Sequelize(config.database, config.username, config.password, config);
}

// ...

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

所以我想使用 TypeScript 做同样的事情。
models/index.ts :

import { Sequelize } from 'sequelize';
import { DbInterface } from '../../@types';

export const createModels = (sequelizeConfig: any): DbInterface => {
  const { database, username, password, params } = sequelizeConfig;
  const sequelize = new Sequelize(database, username, password, params);

  const db: DbInterface = {
    sequelize,
    Sequelize, // <= TSC throw an error here.
  };

  Object.keys(db).forEach((modelName) => {
    if (db[modelName].associate) {
      db[modelName].associate(db);
    }
  });

  return db;
};
@types/dbInterface.ts :

import Sequelize from 'sequelize';

export interface DbInterface {
  sequelize: Sequelize.Sequelize;
  Sequelize: Sequelize.Sequelize;
}

TSC 抛出类型错误:

Type 'typeof Sequelize' is missing the following properties from type 'Sequelize': Sequelize, config, modelManager, connectionManager, and 23 more.



我怎样才能正确地制作类型?

包版本:
"sequelize": "^5.21.3"
"typescript": "^3.7.5"

最佳答案

使用 sequelize-typescript 有问题吗?
它具有您需要的所有定义,因此您无需编写自己的定义。

如果您在使用 sequelize-typescript 时遇到问题,只需安装 @types/sequelize 即可获取这些类型。

关于node.js - 'typeof Sequelize' 类型缺少 'Sequelize' : Sequelize, 配置、modelManager、connectionManager 等 23 个类型中的以下属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59877993/

相关文章:

node.js - 向我的 sequelize 查询添加限制会引发错误

javascript - 在 Mocha 中 before 的范围问题

node.js - node-archiver:归档多个目录

node.js - Express 中的嵌套错误处理

mysql - 风 sails js : select records from mysql?

javascript - 无法绑定(bind)到 '',因为它不是 '' angular 2 的已知属性

javascript - 如何通过 npm TypeScript 编译器生成 JavaScript 包

node.js - 简单的 Express 3.x socket.io 聊天

node.js - Sequelize 方言问题

javascript - 无法从 sequelize 中的 select 语句中排除关联的字段