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!

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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 /// <reference path="Foo" />, 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
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?
What kind of module are you trying to create? CommonJS?
I think the answer to your question is here: stackoverflow.com/questions/26629340/…
I added the comment to your post, so others might see it as well. Thank you, this gets the job done :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.