使用类型信息导出typescript变量

时间:2016-10-04 01:32:02

标签: node.js typescript

我有一个带接口的配置变量。

//I declare my interface

interface ServerConfiguration {
  development: {
    db: {
      url: string,
      user: string,
      password: string,
    },
    webServer: {
      port: number;
      rootPath: string;
    }
  };
}

//I declare a variable that uses that interface

const config: ServerConfiguration = {
  development: {
    db: {
      url: "someUrl",
      user: "myuser",
      password: "mypassword",
    },
    webServer: {
      port: process.env.PORT || 9001,
      rootPath: rootPath,
    },
  },
};
// I export my variable
export  {config};

之后我导入它并尝试创建一个旨在使用该配置的函数

// I import my variable
import  config  = require( "./config");


// I create a function that will consume a variable that complies with that interface
function initializeDatabase(config: ServerConfiguration) {
//do stuff
}

我在这里得到错误"找不到名称' ServerConfiguration'",出了什么问题?

1 个答案:

答案 0 :(得分:3)

  

在这里我得到错误“找不到名字'ServerConfiguration'”,出了什么问题?

  • 单独导出每件事

    export interface ServerConfiguration {
    // also 
    export const config: ServerConfiguration = { 
    
  • 并导入

    import  {config,ServerConfiguration} from "./config";