Reputation: 2317
I have found this strange case in my NodeJS project. I will try to describe my problem here believing that there's a simple option in tsconfig.json
exactly for this case. I use TypeScript v1.7.3.
File test1.ts
contains variable declaration:
// test1.ts
let a = 1;
File test2.ts
contains improper variable usage:
// test2.ts
console.log(a);
tsconfig.json
looks like this:
// tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es5"
}
}
Compiler does not throw an error to me that I am using undeclared variable a
. But if I try to export some other variable, say b
we will get expected error:
// test1.ts
let a = 1;
export let b = 2;
Compiler:
Error:(1, 13) TS2304: Cannot find name 'a'.
How can I make compiler emit an error in the first case? I've just found out in my project that I suddenly removed a variable and it failed in runtime, not compile time.
Upvotes: 1
Views: 1030
Reputation: 23443
This is an unfortunate consequence of the fact that files without some kind of export or import are considered "script" files by the compiler. The compiler assumes script files just operate in the global scope, and will just get stitched together. Without --outFile
being specified, it won't be able to definitely say whether a variable declaration will occur after its usage.
One workaround is to just add a
export {};
statement to your files.
Upvotes: 1