Joaquin Brandan Joaquin Brandan - 2 months ago 14
TypeScript Question

exporting a typescript variable with its type information

I have a certain config variable with an interface.

//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};


after that i import it and try to create a function that is designed to consume that configuration

// 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
}


Here i get the error "cannot find name 'ServerConfiguration'", what's wrong?

Answer

Here i get the error "cannot find name 'ServerConfiguration'", what's wrong?

  • Export each thing individually

    export interface ServerConfiguration {
    // also 
    export const config: ServerConfiguration = { 
    
  • and import it

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