简体   繁体   English

Typescript 的“对象”类型错误中不存在属性

[英]Property does not exist on type 'Object' Error with Typescript

I made config.ts file as below.我制作了 config.ts 文件如下。

export interface IDBConfig {
  username: string;
  password: string;
  database: string;
  host: string;
  dialect: string;
}

class Config {
  private DBConfig: IDBConfig;

  constructor() {
    this.DBConfig = {
      username: "",
      password: "",
      database: "",
      host: "",
      dialect: "",
    };
  }

  public getDBConfig(environment: string): Object {
    switch (environment) {
      case "local":
        this.DBConfig = {
          username: "root",
          password: "1234",
          database: "test",
          host: "127.0.0.1",
          dialect: "mysql",
        };
        break;
    }
    return this.DBConfig;
  }
}

export { Config };

And I imported this config in sequelize.ts file.我在 sequelize.ts 文件中导入了这个配置。
'console.log(dBConfig) gives me the object of DBConfig without problem. 'console.log(dBConfig) 给我 DBConfig 的 object 没有问题。
But the next line gives me a error says property 'database' does not exist on type 'Object'.但是下一行给了我一个错误,说“对象”类型上不存在属性“数据库”。
'username', 'password', 'host' also have same problem. 'username', 'password', 'host' 也有同样的问题。

import { Sequelize } from "sequelize";
import { Config } from "./config";

class SequelizeRun {
  private sequelize: object;

  constructor() {
    this.sequelize = {};
  }

  public getSequelize() {
    const config = new Config();
    const dBConfig = config.getDBConfig("local");

    console.log(dBConfig);

    this.sequelize = new Sequelize(
      dBConfig.database,
      dBConfig.username,
      dBConfig.password,
      {
        host: dBConfig.host,
        dialect: "mysql",
        timezone: "+00:00",
        pool: {
          max: 30,
          min: 0,
          acquire: 30000,
          idle: 10000,
        },
      }
    );
  }
}

Could you give me some solution for this?你能给我一些解决方案吗? Thank you for reading this.谢谢您阅读此篇。

public getDBConfig(environment: string): IDBConfig {
    switch (environment) {
      case "local":
        this.DBConfig = {
          username: "root",
          password: "1234",
          database: "test",
          host: "127.0.0.1",
          dialect: "mysql",
        };
        break;
    }
    return this.DBConfig;
  }

try this one.试试这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM