Question

I am trying to add a plugin to the TypeScript compiler and so I added my code and I compiled tsc.ts. It compiles correctly, but when I run, it's missing some variables that are declared in io.ts. I removed my changes, and it still doesn't work. So I tried this:

file: a.ts

var a : number = 5;

file: b.ts

///<reference path='a.ts' />
console.log(a);

and then compile: tsc b.ts which gives me a.js and b.js. When I try to run b.js (I do node b.js), the variable a is undefined. This is what the content of b.js:

///<reference path='a.ts' />
console.log(a);

and so it makes sense that a is undefined, since a is nowhere in this file and so is a.ts.

Am I compiling it wrong, or executing it wrong.. or what?

Was it helpful?

Solution

If you're running under node, you should use export and import to manage cross-file dependencies.

a.ts

export var x = 5;

b.ts

import a = require('./a');
console.log(a.x);

Compile with --module commonjs

If you were running on the web you could use --outFile to concatenate into a single file, but that's usually not a good option under node.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top