1

I'm building a typescript library, and I would like to do it the following way:

  1. One class (interface) / file in the dev sources
  2. Since the whole library "belongs together", all can go under 1 namespace
  3. When building, I want to be able to create a single output js and d.ts file, only with the relevant exports/definitions.

The way I tried to do is the following:

I used the /// <reference path="..." /> syntax, to reference other files

Foo.ts

class Foo {
    // stuff
}

Bar.ts

/// <reference path="Foo" />

class Bar extends Foo {
    // just an example for referencing another compilation unit
}

Create a tsconfig.json file, which uses the out option, so everything is concatenated into a single file:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "ES5",
        "out": "src-gen/app.js"
        /* other options ... */
    }
}

And finally add a single file, which exports all the stuff, what I want to be able to import from other files, like this:

Exports.ts

/// <reference path="Bar" />

export {Bar} // don't want to expose Foo, since its not relevant for users of the lib.

But the export {Bar} line gives the following compile error:

Circular definition of import alias 'Bar'. import Bar

I have no idea, what this error means, since Bar is not imported anywhere, only referenced using the /// <reference> syntax.

My whole goal at the end is, to have a single file, js file as output, which looks like this:

/* compiled js source of Foo */

/* compiled js source of Bar */

exports = {
    Bar
}

My question(s) are:

  1. Is this a valid way to do things?
  2. If yes, what's the problem with export {Bar}
  3. If no, how would you achieve, what I want to do?

Thanks in advance, for any help/advice!

Balázs Édes
  • 13,452
  • 6
  • 54
  • 89

1 Answers1

2

I think what you want to do is use modules:

Foo.js

module MyModule {
  class Foo {
      // stuff
  }
}

Bar.js

module MyModule {
  export class Bar extends Foo {
      // stuff
  }
}

Then you don't need Exports.js, since just the items marked with export get exported.

I believe that, if you compile all the files at once, you won't need the /// <reference lines, either.

Edit: I think the answer to your question is here: Creating a single CommonJS module from several TypeScript classes

Community
  • 1
  • 1
David Norman
  • 19,396
  • 12
  • 64
  • 54
  • Hi, thanks for the answer, but are you sure, this should work? When I do exactly the same, I get `Module 'MyModule' has no exported member 'Foo'`. Even if I add `/// `, or try to reference `MyModule.Foo` with fully qualified name (`... extends MyModule.Foo`). The error is the same from VS Code, and from command line. I forgot to mention, but I'm using typescript 1.5 beta – Balázs Édes May 30 '15 at 07:32
  • Update! I have been playing around with it, and if I export `Foo` too, then it's visible in `Bar.ts`, which is almost what I want, and for now its more than OK. But how do I reference classes from `MyModule` outside of the namespace? I tried `import Bar from 'Bar'` and `import MyModule from 'Bar'`, also `import MyModule from 'Bar'`, but I get `File Bar.ts is not an external module`. What's the deal here? – Balázs Édes May 30 '15 at 07:45
  • What kind of module are you trying to create? CommonJS? – David Norman Jun 01 '15 at 20:55
  • I think the answer to your question is here: http://stackoverflow.com/questions/26629340/creating-a-single-commonjs-module-from-several-typescript-classes – David Norman Jun 02 '15 at 14:28
  • I added the comment to your post, so others might see it as well. Thank you, this gets the job done :) – Balázs Édes Jun 02 '15 at 14:44