我正在使用带有 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] Property 'production' does not exist on type 'typeof "filepath/dbconf"'.
我已经声明了要导出的变量 configs
的接口(interface)和类型。我不明白为什么我仍然收到此错误。
我这里唯一的限制是 dbconfig.ts 必须直接导出配置对象,而不是导出时的某些属性。我在其他文件中也面临同样的问题。 TS中导入导出文件的正确方法是什么?
谢谢。
最佳答案
您已导出默认
:
export default configs
但是您的导入语句 import * as SequelizeConfig from './dbconfig';
是错误的。默认导出的正确导入:
import sequelizeConfig from './dbconfig';
更多
我个人不推荐默认值:https://basarat.gitbooks.io/typescript/docs/tips/defaultIsBad.html
关于javascript - 导入的 typescript 对象没有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47213159/