Reexport class in Typescript

Typescript

Typescript Problem Overview


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?

Typescript Solutions


Solution 1 - Typescript

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

Solution 2 - Typescript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmlekoView Question on Stackoverflow
Solution 1 - TypescriptmlekoView Answer on Stackoverflow
Solution 2 - TypescriptLeonView Answer on Stackoverflow