导入的打字稿对象不具有该属性

我用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]types'typeof“filepath / dbconf”'中不存在属性“生产”。

我已经声明了我正在导出的variablesconfigs的接口和types。 我不明白为什么我仍然得到这个错误。

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

谢谢。

您有导出default

 export default configs 

但是,您的导入语句import * as sequelizeConfig from './dbconfig'; 是错的。 正确的导入默认导出您将:

 import sequelizeConfig from './dbconfig'; 

更多

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