Search code examples
typescriptcode-separation

TypeScript - separating code output


i'm trying typescript and I find it very useful.

I've a quite large project and i was considering rewriting it using typescript. The main problem here is the following:

file A.ts:

class A extends B {
    // A stuff
}

file B.ts:

class B {
    // B stuff
}

If I compile A.ts with this command:

tsc --out compiledA.js A.ts

I'll get error from the compiler cause he doesn't know how to threat the "B" after extends.

So, a "solution" would be including in A.ts (as first line of code):

/// <reference path="./B.ts" />

Compiling again A.ts with the same command

tsc --out compiledA.js A.ts

Will result in compiledA.js containing both B.ts and A.ts code. ( which could be very nice )

In my case, I only need to compile the A.ts code in the compiledA.js file and I don't want the B.ts stuff to be in there.

Indeed, what I want is:

  • tsc --out A.js A.ts => compile only the A.ts stuff
  • tsc --out B.js B.ts => compile only the B.ts stuff

I can do it by removing the "extends" keyword but doing that I'll loose most of the typescript goodness.

Can someone telll me if there's a way to do this ?


Solution

  • After some research I found out the problem was introduced by the --out argument in the compiler.

    @silent__thought solution works just fine if you want to deal with modules. If you're not you'll need to use the require statement ( look at the question ) and then compile the "main.ts" file withouth the --out argument