Import and export classes in Typescript

I currently have two separate classes defined in two different files.

//a.ts
export class A{}

//b.ts
export class B{}

My goal is to create a new file c.ts where I can import both classes seamlessly.

import {A, B} from "c";

Instead of having to import them individually like this:

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

I'm looking to create a sort of export facade. How can I achieve this reexporting of types?

Answer №1

I managed to discover the solution on my own

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

Here is the code I used to achieve my desired outcome

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

Default export

If you have a file like this

//d.ts
export default class D{}

The re-export should be structured as follows

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

or

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

What this does is specify that you want to re-export the default export from the module "D" but with the name D

Answer №2

If you want to avoid using the default export, you can re-export an imported module by following this pattern:

import {X} from './X.js';
import {Y} from './Y.js';

const Z = 'another variable'

export {X, Y, Z}

By doing this, you are able to export all the variables that were either imported or declared within this particular file.

Resource: source

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The ActivatedRoute snapshot does not function properly when used in the TypeScript class constructor

Currently, I am encountering a challenge with TypeScript and Angular 2. The structure of my TS class is as follows: 'import { Component, OnInit } from '@angular/core'; import {ActivatedRoute} from '@angular/router'; @Component({ ...

Limiting the Rate of Requests to a TCP Server using net.Server

I've been utilizing net.Server as my TCP server. Is there a way to impose a message rate limit? I managed to find solutions for enforcing rate limits in Express (express-rate-limit) and Websocket (websocket-rate-limit), but nothing specifically for ...

Executing callback in the incorrect context

I'm facing an issue and can't seem to navigate through it. I am setting up a callback from a third party directive, but the callback is not returning with the correct scope. This means that when it reaches my controller, this refers to some other ...

Guide to transforming JSON data into a different structure

My API is currently providing data in this format: [ { "lattitude": 52.57812538272844, "longitude": -1.7111388218750108, }, { "lattitude": 53.043884, "longitude": -2.923782, } ] I need to transform the data ...

Steps to allow an ng-model to accept a variety of input values

Imagine having an input box structured like this <ion-input [(ngModel)]="Gender" type="text" placeholder="Gender Type"></ion-input> <ion-input [(ngModel)]="hairCat" type="text" placeholder="Hair Type"></ion-input> Now, let's ...

Sequelize v5 & Typescript Model Loader

Having previous experience with Sequelize for projects (v4), I am now venturing into starting a new project using Sequelize v5 & Typescript. I have been following Sequelize's documentation on how to define Models at: https://sequelize.org/master/ ...

Unable to Add Dependency to Subclassed Object

In my setup, there are three classes that interact with each other. I am utilizing inversify for handling dependency injection. However, I encountered an issue when I injected the class MessageBroker into the derived class Repository and found that the Mes ...

What happens when Angular elements don't have an injector?

Exploring Angular's custom elements while steering clear of dependency injection is my current experiment. const MyElementElement = createCustomElement(MyElementComponent, { injector }); customElements.define('my-element', MyElementElement) ...

Navigate the nested route of a child page starting from the main Root component

I am facing an issue with enabling nesting routes on the BarcodeScannerComponent component. I have attempted the following method, but it does not seem to work. The main challenge lies in accessing the nested route within the root component, which is app.c ...

The placement of the FirebaseAuth.onAuthStateChanged call in an Angular application is a common concern for developers

Where is the best place to add a global listener initialization call in an Angular app? Take a look at this code snippet: export class AuthService { constructor( private store: Store<fromAuth.State>, private afAuth: AngularFireAuth ) { ...

Does the term 'alias' hold a special significance in programming?

Utilizing Angular 2 and Typescript, I have a component with a property defined as follows: alias: string; Attempting to bind this property to an input tag in my template like so: <input class="form-control" type="text" required ...

There seems to be a contradiction in my code - I am returning a Promise but TypeScript is throwing an error saying that the

I currently have a function that retrieves a bot's inventory on the Frontend fetchBotInventory() { this.socket.emit('fetch bot inv'); this.socket.on('bot inv', (botInventory) => { return new Promise((resolve, re ...

ESlint is unable to parse typescript in .vue files

After using vue ui to build my project, here is the content of my package.json: "@vue/cli-plugin-eslint": "^4.1.0", "@vue/cli-plugin-typescript": "^4.1.0", "@vue/eslint-config-standard": "^4.0.0", "@vue/eslint-config-typescript": "^4.0.0", "eslint": "^5.1 ...

Utilize Typescript to generate an object that contains a partial interface

Seeking assistance with an issue. We are working with two interfaces: interface IUserEntity { Id: number; FirstName: string; LastName: string; } interface IUserEntityMethods { GetFullName(): string; } I am trying to create an object tha ...

Conceal a specific segment on the web page if the API call in Angular does not return any data

I'm currently working with data retrieved through an API call and I need assistance in implementing code to hide a section when there is no data being fetched. Could you provide a sample code for this? ...

Is it possible to target a specific element using Angular2's HostListener feature? Can we target elements based on their class name?"

Is there a way in Angular2 to target a specific element within the HostListener decorator? @HostListener('dragstart', ['$event']) onDragStart(ev:Event) { console.log(ev); } @HostListener('document: dragstart' ...

I'm having trouble displaying the saved percentage value in an angular material mat select component

When I use the mat-form-field and mat-option to list numbers 1-100, the saved value is not displaying in the dropdown after saving. <mat-form-field class="full-wid" appearance="outline"> <mat-label>Percentage 1 (%)</mat-la ...

Retrieve the mfData value from the TypeScript file in order to perform operations on it within the Angular 2 framework

I have a snippet of code that iterates through data from stacklist_table, which is a JSON array, and displays it in a table format. The stacklist_table contains a full list of objects, but I only need a subset of these objects so I have applied some filter ...

The module 'contentlayer/generated' or its type declarations are missing and cannot be located

Currently running NextJS 13.3 in my application directory and attempting to implement contentlayer for serving my mdx files. tsconfig.json { "compilerOptions": { ... "baseUrl": ".", "paths" ...

Connect one router to another router within the Oak framework, similar to how it is done in

I have a goal of setting up multiple routers along with a main router that can route requests to the other routers. router.use("/strategy", strategyRoutes); router.use("/account", accountRoutes); The objects router, strategyRoutes, and ...