Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"reexport" a module, aka Ability to partition an ambient module across several files #276

Closed
fsoikin opened this issue Jul 28, 2014 · 2 comments
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript

Comments

@fsoikin
Copy link

fsoikin commented Jul 28, 2014

Sometimes it happens so that some code logically belongs in one module, but there is just too much of it to put in one file. For example, here is my use case:

// base.ts
export interface SomeCoreType { ... }

export interface MyStandardAjaxResponse { ... }
export function doAjax( url: string, onSuccess: (r: MyStandardAjaxResponse) => void ) { ... }

export interface User { ... }
export function isUserOk( u: User ) { return ... }
// and so on
// customer.ts
import b = module( "./base.ts" );

export class Customer {
    constructor( id: number, someArg: b.SomeCoreType ) {
        b.doAjax( '/api/getCustomer', r => this.loadData( r ) );
    }

    loadData( r: b.MyStandardAjaxResponse ) { ... }
}
// and so on

Over time, that base.ts file tends to grow with all kinds of enums, utility functions, common types, etc. At some point, it becomes so large that I want to partition it across several files. For example, I'd like to extract SomeCoreType into its own module coreType.ts, then extract MyStandardAjaxResponse and doAjax into ajax.ts, and extract User and isUserOk into user.ts. Just for my sanity's sake. Makes sense?

But if I do that, I'll have to import a gazillion of modules every time.
So my next logical step - consolidate them all back into base.ts:

// base.ts
import _coreType = module("./coreType");
export var CoreType = _coreType;

import _ajax = module("./ajax");
export var Ajax = _ajax;

import _user = module("./user");
export var User = _user;
// and so on

That's better. Now I only have to import base.ts.
However, I still have to write b.Ajax.doAjax instead of just b.doAjax. That's inconvenient.
But an even bigger issue is that now I can't use interfaces declared in those modules. TypeScript just doesn't pick them up.

What I'd like to be able to do is something like this:

import Ajax = module("./ajax");
export Ajax.*;

Or, perhaps, the infamous export import:

export import Ajax = module("./ajax");

Or, maybe, something like:

reexport module("./ajax");

And the output code could look something like this:

define(['module', 'exports', './ajax'], function(module, exports, __ajax__) {
    for( var k in __ajax__ ) exports[k] = __ajax__[k];
});
@RyanCavanaugh
Copy link
Member

Can I close as duplicate of #17? Or is this a different use case?

@fsoikin
Copy link
Author

fsoikin commented Jul 28, 2014

Yes, the use case is exactly the same.
Although, I must note that my proposal (i.e. have a separate "bundling" file) nicely solves the "module boundaries" issue.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants