Namespacing exports as
Currently to export things under a conceptual "package" namespace in TypeScript developers must add a proxy barrel file
a.ts
export const a = 'foo'
b.ts
export const b = 'bar'
index.barrel.ts
export * from './a'
export * from './b'
index.ts
export * as mypackage from './index.barrel'
It would be great if the `export * as mypackage' statement could merge exports with the same name
a.ts
export const a = 'foo'
b.ts
export const b = 'bar'
index.ts
export * as mypackage from './a'
export * as mypackage from './b'
My suggestion meets these guidelines:
@rbuckton thoughts?
This seems like something that should be proposed to TC39 first, as this directly affects the ECMAScript syntax.
I have been interested in extending more of the ECMAScript module syntax to namespaces, but that's been fairly low on the list of priorities. If we did that however, you might be able to do something like this:
import * as a from './a';
import * as b from './b';
export namespace mypackage {
export * from a;
export * from b;
}
If all you're interested in is exporting values, you could also write
import * as a from './a';
import * as b from './b';
export const mypackage = { ...a, ...b };
This seems like something that should be proposed to TC39 first, as this directly affects the ECMAScript syntax.
I have been interested in extending more of the ECMAScript module syntax to namespaces, but that's been fairly low on the list of priorities. If we did that however, you might be able to do something like this:
import * as a from './a'; import * as b from './b'; export namespace mypackage { export * from a; export * from b; }
I have needed and wanted this syntax dozens of times. Specifically, namespace/function/type merging is useful for representing things like a type User, component function User, and namespace User (i.e. User.someFunction).
It's extremely hard currently to re-export something like User.Profile if Profile exists outside of the namespace definition itself.
Most helpful comment
This seems like something that should be proposed to TC39 first, as this directly affects the ECMAScript syntax.
I have been interested in extending more of the ECMAScript module syntax to namespaces, but that's been fairly low on the list of priorities. If we did that however, you might be able to do something like this: