导入的typescript对象没有该属性

时间:2017-11-09 23:10:57

标签: javascript node.js typescript

我正在使用带有Typescript的nodeJs:

这就是我在dbconfig.ts中所拥有的

interface DatabaseConfig {
  username: string;
  password: string;
  database: string;
  host: string;
  dialect: string;
  port?: number;
  logging?: boolean | Function;
  force?: boolean;
  timezone?: string;
}

interface DatabaseConfigs {
  development: DatabaseConfig,
  staging: DatabaseConfig,
  production: DatabaseConfig
}

 var configs: DatabaseConfigs = {
  development: {
    username: "postgres",
    password: "password",
    database: "development",
    host: "localhost",
    dialect: "postgres"
  },
  staging: {
    dialect: "postgres",
    database: "development",
    username: "postgres",
    password: "password",
    host: "localhost"
  },
  production: {
    dialect: "postgres",
    database: "development",
    username: "postgres",
    password: "password",
    host: "localhost"
  }
};

export default configs;

当我在config.ts中导入此文件时,如下所示:

import * as sequelizeConfig from './dbconfig';

然后在同一个文件中尝试访问

sequelizeConfig.production;

TS提出错误说:

  

[ts]类型'typeof上不存在属性'生产'   “文件路径/ dbconf””。

我已经声明了我要导出的变量configs的接口和类型。我不明白为什么我仍然会收到此错误。

我在这里唯一的限制是dbconfig.ts必须直接导出configs对象而不是导出时的某些属性。我也在其他文件中面临同样的问题。在TS中导入和导出文件的正确方法是什么?

谢谢。

1 个答案:

答案 0 :(得分:0)

您已导出default

export default configs

但您的导入声明import * as sequelizeConfig from './dbconfig';是错误的。正确导入默认导出:

import sequelizeConfig from './dbconfig';

更多

我个人不推荐默认值:https://basarat.gitbooks.io/typescript/docs/tips/defaultIsBad.html