Compile multiple typescript into one closure

When compiling (-out) multiple * .ts files, each containing one class with the same module ID, a * .js file is created where each class is wrapped in its own closure, and a module reference is passed to each closure.

Example:

Bar.ts:

module baz {export class Bar {}}

      

Foo.ts:

module baz { export class Foo {}}

      

out.js

var baz
(function(baz){ var Bar = ... })(baz);
(function(baz){ var Foo = ... })(baz);

      

Is there a way to include both classes in one file so that the compiler creates one closure in which both classes are declared? Placing all classes in one * .ts file seems like a very bad idea.

The goal is as follows:

  • Use one * .ts file per class
  • Use non-exported classes in all classes of the module
  • Provide a clean interface for the module

Dart-like pseudocode what I actually mean:

baz.ts

module baz {
    part "Foo.ts";
    part "Bar.ts";
}

      

Foo.ts

part of "baz.ts";
export class Foo {}

      

Bar.ts

part of "baz.ts";
export class Bar {}

      

Thanks for reading,

Martin

+3


source to share


1 answer


In TypeScript, the glue point for your question is point 2. If you split your module into multiple files, you will need to export the classes that you want to make available to other files.



Of course, this also means that consumers will see all of the classes listed, which I understand affect your meaning of providing a clean interface to the module. The answer to structuring your modules and classes within constraints will differ depending on your individual circumstances. I tend not to think of modules as broad namespaces, I make them rather narrow and specific, so I would have a module Ajax

, not a module Communication

with different classes to represent different ways of communicating.

+1


source







All Articles