Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregate imports then export in TypeScript

Tags:

typescript

I'm used to using ES6 syntax. I tend to aggregate my React Component files into one "import" file which I use as the index.js of my Components folder. This allows me to write destructing imports like so:

import { App, Ticker, Timer } from "./components"

Inside of my index.js file I can easily write export from import commands like so:

export App from "./app"
export Ticker from "./ticker"
export Timer from "./timer"

Is there a way I can do this in TypeScript? I can't seem to get index.ts to be recognized as the default file in a module. Is there also any syntax to export an import in TypeScript?

like image 289
Navx Avatar asked Mar 07 '16 05:03

Navx


People also ask

What is export {} in TypeScript?

TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.

How do I export multiple interfaces in TypeScript?

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

Can we export function in TypeScript?

Yes, we can export the functions in TypeScript by using the 'export' keyword at the start of the function. export interface inteface_name: In TypeScript, we can export the interface as well, we can follow this syntax to make the interface exportable.

How do I export a TS object?

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.


1 Answers

Is there a way I can do this in TypeScrip

Just use the es6 export * syntax:

export * from "./components";

Alternatively you can still do:

export {App,Ticker,Timer} from "./compoents"
like image 161
basarat Avatar answered Oct 09 '22 15:10

basarat