104

I have two classes in two files.

//a.ts export class A{} //b.ts export class B{} 

How I can build file c.ts from which I could import both classes?

import {A, B} from "c"; 

instead of

import {A} from "a"; import {B} from "b"; 

I want to make kind of export facade. How to reexport type?

2 Answers 2

181

I found answer by myself

https://www.typescriptlang.org/docs/handbook/modules.html @Re-exports

Code to do what I wanted

//c.ts export {A} from "a"; export {B} from "b"; 

Default export

Assuming you have file

//d.ts export default class D{} 

Re-export have to look like this

//reexport.ts export { default } from "d"; 

or

//reexport.ts export { default as D } from "d"; 

What happens here is that you're saying "I want to re-export the default export of module "D" but with the name of D

Sign up to request clarification or add additional context in comments.

2 Comments

and what if A and B are default (and not named) exports? export A from 'a' complains declaration or statement required
@dark_ruby you have to import and export on separate statements, updated answer
16

For anyone not wanting to deal with default you re-export an imported module like so

import {A} from './A.js'; import {B} from './B.js'; const C = 'some variable' export {A, B, C} 

This way you can export all the variables you either imported or declared in this file.

Taken from here

6 Comments

If you don't need A/B in this file: export { A } from './A'; export { B } from './B'; export const C = '...'
@charles-allen Could you elaborate on your comment ? I don't understand what you mean by "If you don't need...". Isn't the purpose of this example to show how to reexport them ? If you don't need them then you are just making an plain export
Just to be clear in case that's not obvious, you don't need to declare a third variable to export in your file. Your file can only import a bunch of types and reexport them all.
@cassepipe - It's possible to create a facade that re-exports things from multiple source modules without a separate import step (the 3 lines I posted above does the same as the 4 lines in the current solution by immediately reexporting).
@charles-allen I haven't done anything, tha'ts not my solution. Thanks very much for the clarification.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.