Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I export an object in TypeScript?

For example, I'm trying to export a TS object to get this JavaScript output:

const path = require('path'),       rootPath = path.normalize(__dirname + '/..'),       env = process.env.NODE_ENV || 'development';  let config = {   development: {     amqpUrl: "amqp://localhost:15672",     root: rootPath        },   test: {     amqpUrl: "amqp://localhost:5672",     root: rootPath      },   production: {     amqpUrl: "amqp://localhost:5672",     root: rootPath    } }; module.exports = config[env]; 

This is my TS, but it's not clear with exporting,

import path = require("path")          const rootPath = path.normalize(__dirname + '/..')     const env = process.env.NODE_ENV || 'development'          let config = {       development: {         amqpUrl: "amqp://localhost:15672",         root: rootPath                },       test: {         amqpUrl: "amqp://localhost:5672",         root: rootPath              },       production: {         amqpUrl: "amqp://localhost:5672",         root: rootPath            }     };     /* this is the line i'm having problem how can i export config object*/     // export config[env]; 

I've tried export default config[env] but its generated output isn't expected. What am I doing wrong?

like image 366
Gayan Avatar asked Jan 20 '17 11:01

Gayan


People also ask

How do I use exported interface TypeScript?

Use a named export to export an interface in TypeScript, e.g. export interface Person{} . The exported interface can be imported by using a named import as import {Person} from './another-file' . You can have as many named exports as necessary in a single file.

Can we export function in TypeScript?

YES, TypeScript can export a function! "Any declaration (such as a variable, function, class, type alias, or interface) can be exported by adding the export keyword."

What is export {} in TypeScript?

The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum. When exporting a module using export = , TypeScript-specific import module = require("module") must be used to import the module.

How do I export objects in TypeScript?

TypeScript has export = syntax. It specifies a single object that is exported from the module. This can be a function, class, interface, namespace, or enum.


2 Answers

In ES6 you are allowed to export names using the export function, or for default you can export anything. The require format goes like this:

let config = require('config') 

And it takes the default export of config file. In your case, you should do:

export default config[env] 

If you want to use the export, you would do something like:

let Environment = config[env]; export {Environment} 

The difference would be:

import EnvirmentNameWhatever from "./config" 

to

import {Environment} from "./config" 
  • Note - when default exporting, you can use whatever name you like, while when just exporting, you have to use the exported name.
like image 50
Amit Avatar answered Oct 06 '22 03:10

Amit


Using the export keyword on the declarations to export should do the job, like this:

import path = require("path")  const rootPath = path.normalize(__dirname + '/..') export const env = process.env.NODE_ENV || 'development'  export let config = {     development: {     amqpUrl: "amqp://localhost:15672",     root: rootPath      },     test: {     amqpUrl: "amqp://localhost:5672",     root: rootPath      },     production: {     amqpUrl: "amqp://localhost:5672",     root: rootPath      } }; 
like image 23
daragua Avatar answered Oct 06 '22 03:10

daragua