1

Closed

Can I export interfaces together with "export =" statement?

description

Can this be done:
declare module "foo"
{
    export interface barOptions { ... }

    class bar
    {
        constructor(options?: barOptions);
    }

    export = bar;
}
This will make the module completely self-contained. Currently, whenever there is an "export=" statement, the keyword "export" is not allowed on variables, classes and interfaces.

I strongly suggest that export be allowed on interfaces together with an "export =" statement.

This way, the user of the module can do away with an extra .d.ts file just to define the interface which is used only within the module itself:
import xyz = require("foo");
var r = new xyz(<xyz.barOptions> { ... });
Closed Aug 8, 2013 at 7:49 PM by danquirk
You say:

"This way, the user of the module can do away with an extra .d.ts file just to define the interface which is used only within the module itself"

but that isn't true, which is exactly why you need export or a .d.ts depending on the circumstances. If the interface was indeed only used within the module then all would be well, but as a constructor parameter for an exported type the interface is part of the public surface area of your module, so it is not only used within the module itself as any consumer of your module will need to know the type definition for the interface.

If this was allowed then there'd be some additional inconsistency and complication for if your constructor parameter were instead a class type (or an interface which extended a class).

comments

schungx wrote Aug 9, 2013 at 3:21 AM

Oh. You're probably right about the possibility that the interface extends a class. However, if it is only an interface (which I believe 99% of the case it will be if it is just an options type object), then allowing it to export will save an extra .d.ts file.

Of course, if the user want to use this options interface in another code file which does not import the module, then he will have a problem... I understand your point here.