javascript - 导入的 typescript 对象没有属性

标签 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] 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/

相关文章:

javascript - 使用正则表达式在 <p> 标签中查找除 anchor 标签以外的所有内容

javascript - 以 4 倍速度运行 Javascript 时钟

javascript - 如何在 typescript 中使用传单初始化 SVG 层?

typescript - 类型注释和类型推断有什么区别?

Javascript,更改谷歌地图标记颜色

javascript - react Hook : updating state using useState does not update the state immediately

node.js - Twitter 返回未经授权的用户时间线,但不返回搜索

node.js 服务器没有响应

node.js - NodeJS : route. 使用传统查询字符串格式获取

angular - 如何配置 Angular 5 以支持模块别名?