Skip to content

Merge "export * as" exports  #38041

Closed
Closed

Description

@alshdavid

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:

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

Activity

  1. RyanCavanaugh commented on Apr 20, 2020

    @RyanCavanaugh
    Member
  2. rbuckton commented on Apr 20, 2020

    @rbuckton
    Contributor

    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;
    }
  3. DanielRosenwasser commented on Apr 22, 2020

    @DanielRosenwasser
    Member

    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 };
  4. cruhl commented on Jul 21, 2020

    @cruhl

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions