Typescript: Unability to group exports into a namespace or a module

Created on 10 Aug 2017  路  15Comments  路  Source: microsoft/TypeScript

Well, I need to group my exports in two namespaces: "n1" and "n2", so how I'm supposed to do this? I can't find any proper information in official docs, related to such problem. Is there any way to do this?

Example:

// bigModule.ts
export namespace n1{
    export * from './n1/myModule1';
    export * from './n1/myModule2';
    export * from './n1/myModule3';
}

export namespace n2{
    export * from './n2/myModule1';
    export * from './n2/myModule2';
    export * from './n2/myModule3';
}

Expected behavior:

import {n1} from 'bigModule.ts';
import {n2} from 'bigModule.ts';

let foo = new n1.someClass(); 
let bar = new n2.someOtherClass(); 

Actual behavior:
TS1194:Export declarations are not permitted in a namespace

Working as Intended

Most helpful comment

Oh jeez, sorry about that. Apparently Babel is ridiculously aggressive at supporting low-stage proposals. I didn't realize it was so far from standard.

All 15 comments

The example code can currently be expressed already

import * as n1 from './n1/myModule1';
import * as n2 from './n2/myModule2';
export {n1, n2};

import {n1} from './bigModule';

let foo = new n1.someClass();

ES module syntax is probably not something we're looking to support in namespaces.

export * as n1 from './n1/myModule1';

is a valid es export, and really ought to be supported.

@EthanRutherford it is only a Stage 1 proposal.

Oh jeez, sorry about that. Apparently Babel is ridiculously aggressive at supporting low-stage proposals. I didn't realize it was so far from standard.

I updated the example to make more clear what solution I've looking for.

@FlamesoFF consider using the spread operator

import * as m1 from './m1';
import * as m2 from './m2';
import * as m3 from './m3';

export const n1 = {...m1, ...m2, ...m3};

@aluanhaddad It's not working.
TS2503:Cannot find namespace 'n1'

Works for me. I tested it. Make sure you are using latest TypeScript and are inside of a .ts or .tsx file and not a .d.ts file.

@aluanhaddad good for you. Anyway, it still not working for me.
I'm using TS v.2.4.2. And code is inside .ts file.
After:

import {n1} from './module';
n1.MyProperty;

Result:
TS2339:Property 'MyProperty' does not exist on type '{}'

@aluanhaddad I finnally made it working:

// myModule.ts
import * as m1 from './m1';
import * as m2 from './m2';
import * as m3 from './m3';

export {m1, m2, m3 as n1};

And the usage:

// test.ts
import {n1} from './myModule';

let foo = new n1.someClass(); // works fine.

Guys, you even don't know how your own product is working. That's ridiculous :)))

@FlamesoFF

Guys, you even don't know how your own product is working. That's ridiculous :)))

Firstly I am not a member of the TypeScript team so do not generalize whatever perception you have of my ignorance to anyone else.

Secondly, my example still works.

// re-exports.ts
import * as m1 from './m1';
import * as m2 from './m2';
import * as m3 from './m3';
export const n1 = {...m1, ...m2, ...m3};

consumed as

import {n1} from './re-exports';
n1. // any member of `m1`, `m2`, or `m3`

Thirdly, civility is always appreciated.

@aluanhaddad Ok, sorry then. Sadly, your example is not working for me.
I'm rude, because the TS docs is lack of information related to the topic and TS team reacts very slowly to majority of issues. That does interfere with productivity.
Still, thanks for the avdice.

If you can create a github repo demonstrating the issue, I would be happy to take a look.

If you can't avoid being rude because of turnaround time on the issue tracker, we would ask that you simply not show up in the first place. Three years ago we were handling 150 reports / month, it's now up to nearly 500 reports / month, and nearly all of that increase has been "noise" issues like questions, duplicates, by-designs, etc..

Was this page helpful?
0 / 5 - 0 ratings