Typescript: Merge "export * as" exports

Created on 19 Apr 2020  路  4Comments  路  Source: microsoft/TypeScript

Search Terms

Namespacing exports as

Suggestion

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

Examples

a.ts

export const a = 'foo'

b.ts

export const b = 'bar'

index.ts

export * as mypackage from './a'
export * as mypackage from './b'

Checklist

My suggestion meets these guidelines:

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [ ] This wouldn't change the runtime behavior of existing JavaScript code
  • [ ] This could be implemented without emitting different JS based on the types of the expressions
  • [ ] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [ ] This feature would agree with the rest of TypeScript's Design Goals.
Out of Scope Suggestion

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:

import * as a from './a';
import * as b from './b';
export namespace mypackage {
  export * from a;
  export * from b;
}

All 4 comments

@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.

Was this page helpful?
0 / 5 - 0 ratings