Exporting symbols within the same namespace from multiple files in a TypeScript project

I have a typescript module and I am looking to define symbols within the 'aaa' namespace from multiple files.

Here is an example of what my code looks like:

a.ts:

export namespace aaa {
  export const a = "a";
}

b.ts:

export namespace aaa {
  export const b = "b";
}

index.ts:

export * from "./a";
export * from "./b";

However, when I try to compile my code, I receive the following warning in the second line of index.ts:

TS2308: Module "./b" has already exported a member named 'aaa'. Consider 
explicitly re-exporting to resolve the ambiguity.

My question is, how can I correctly define symbols within the same namespace across multiple files and then export them all under index.ts?

Answer №1

To consolidate the modules and their individual aaa namespaces, you can utilize the spread syntax.

index.ts

import * as A from "./a";
import * as B from "./b";
export default { ...A.aaa, ...B.aaa };

// or if you prefer a named export
export const aaa = { ...A.aaa, ...B.aaa };

someotherfile.ts

import aaa from "aaa"

const A = aaa.A

It is important to note that the spread syntax performs a shallow merge, meaning that any conflicting declarations between a.ts and b.ts will prioritize b.ts's declaration.

a.ts

export namespace aaa {
  export const foo = "foo";
}

b.ts

export namespace aaa {
  export const foo = "bar";
}

-> someotherfile.ts

import aaa from "aaa"

const A = aaa.foo

console.log(A) // -> 'bar'

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

Exploring the world of node modules and long-term support events

After implementing the following code, I successfully established a connection to the remote server: var fs = require('fs'); var options = { //pem p12 files }; var socket = tls.connect(8000, options, function() { console.log('client co ...

How can we efficiently link data to custom objects (models) within a different class while fetching data from the server using the http.get() method in Angular CLI?

Currently in the process of developing an Angular-Cli application that involves multiple models with relational data tables. When fetching data from the server, I need to map this data to corresponding model objects. I've experimented with creating a ...

The Redux Toolkit slice and TypeScript were not in agreement: it was expecting 0 arguments, but received

Recently, I encountered an issue with my slice code: const investment = createSlice({ name: 'investments', initialState, reducers: { getInvestmentsRequest(state) { state.investments.status = RequestStatuses.loading; }, } }) ...

Select numerous files and conveniently delete them using the angular delete button

Background: In one of my tables, there is a column where users can either choose or upload files as input. I have implemented a feature that allows users to select multiple files at once. Issue at Hand: What I am trying to achieve is to have an 'x&ap ...

Handlebar files are not compatible with Typescript loading capabilities

I am encountering an issue with my directory structure as follows : src |- server |- myServer.ts |- views |- myView.hbs dist |- server |- myServer.js The problem lies in the fact that the dist folder does not have a views subfolder, where the J ...

Exploring depths with Typescript recursion

I'm attempting to implement a recursive search in Typescript, but I am encountering an issue where TS is unable to determine the return type of the function. function findDirectory( directoryId: Key, directory: Directory, ) { if (!directory) ret ...

Issue with applying the React Material UI ThemeProvider

I'm having issues with applying styles using @material-ui/core in my React app. Some of the styles are not being applied properly. You can check out my sandbox for the full code (relevant snippets below). Although I followed the instructions from Ma ...

I am having trouble getting the "npm run build" command to work properly on my React app project

After successfully initializing my react project, I encountered an issue when trying to build it. The error message I received is as follows: node:internal/fs/utils:344 throw err; ^ Error: UNKNOWN: unknown error, lstat 'C:\Users\dar ...

Understanding how to use TypeScript modules with system exports in the browser using systemjs

I’m facing an issue while using systemjs. I compiled this code with tsc and exported it: https://github.com/quantumjs/solar-popup/blob/master/package.json#L10 { "compilerOptions": { "module": "system", "target": "es5", "sourceMap": true, ...

Encountering a problem during the installation of angular-route.d.ts

When trying to install angular-route using the command typings install angular-route --save -global, I encountered an error. Can someone help me resolve this issue? typings ERR! message Unable to find "angular-route" ("npm") in the registry. typings ERR! ...

Instructions on how to dynamically show specific text within a reusable component by utilizing React and JavaScript

My goal is to conditionally display text in a reusable component using React and JavaScript. I have a Bar component that I use in multiple other components. In one particular ParentComponent, the requirement is to show limit/total instead of percentage va ...

Delete a specific element from an array using a specified criteria

I'm attempting to remove a specific item from an array based on the selected option. To better understand, take a look at this code: component.html <fnd-extended-select label="Tipo Prodotto:" [(ngModel)]="landingType" name="tipoprodotto"> ...

Azure DevOps NPM step

While configuring my build pipeline in Azure DevOps, I encountered an issue with the npm run step failing. Specifically, I received the error: "Module build failed (from ./node_modules/@angular-devkit/build-angular/node_modules/sass-loader/lib/loader.js) ...

Encounter an error message "Expected 0 type arguments, but received 1.ts(2558)" while utilizing useContext within a TypeScript setting

Encountering the error mentioned in the title on useContext<IDBDatabaseContext> due to the code snippet below: interface IDBDatabaseContext { db: IDBDatabase | null } const DBcontext = createContext<IDBDatabaseContext>({db: null}) Despite s ...

Tips for picking out a particular item from a list of child elements

When I select the first parent's children array, it ends up selecting every other parent's children as well. This is due to index 0 remaining the same for all of them. How can I specify and highlight a specific child? Link: Check out the stackb ...

Just acquired npm package: The process of including a bearer token in a POST request

Lately, I discovered that request has been discontinued, so I decided to use got as the best alternative. I am attempting to make an external API call to a REST Server but I am not sure how to include a Bearer token in the authorization header of the POST ...

What is the reason for recursion not producing a new object as output?

Trying to filter out nodes in a recursion function that iterates through a tree based on the registry property. function reduceNodesRegistry(source: any) { if (!source.registry) return source; return { ...source, children: s ...

Ways to define an interface that can accommodate various interfaces with a specific structure

I am in need of a function that can accept a parameter with interfaces having a specific structure. The parameter should be either a string hash or a string hash along with an attribute string hash, as shown below: { anotherHash: { a: 'a', ...

Getting parameter names (or retrieving arguments as an object) within a method decorator in TypeScript: What you need to know

I am currently working on creating a method decorator that logs the method name, its arguments, and result after execution. However, I want to implement a filter that allows me to choose which parameters are logged. Since the number and names of parameter ...

The power of Vue reactivity in action with Typescript classes

Currently, I am working on a Vue application that is using Vue 2.6.10 along with Typescript 3.6.3. In my project, I have defined a Typescript class which contains some standard functions for the application. There is also a plugin in place that assigns an ...