属性不存在于类型';对象';Typescript错误
我创建了config.ts文件,如下所示属性不存在于类型';对象';Typescript错误,typescript,Typescript,我创建了config.ts文件,如下所示 export interface IDBConfig { username: string; password: string; database: string; host: string; dialect: string; } class Config { private DBConfig: IDBConfig; constructor() { this.DBConfig = { username:
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 };
我在sequelize.ts文件中导入了这个配置'console.log(dBConfig)毫无问题地为我提供了dBConfig的对象。
但下一行给了我一个错误,即类型“Object”上不存在属性“database”。
“用户名”、“密码”、“主机”也有同样的问题
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,
},
}
);
}
}
你能给我一些解决办法吗?谢谢你阅读这篇文章
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;
}
试试这个。您声明了
getDBConfig()
以返回Object
。它没有数据库
属性。