3

I'm trying to compile a typescript into JavaScript using tsc node package module. First of all, I've installed the module using npm install -g typescript. In my local directory I've created a file called classes.js containing a valid typescript code. When running tsc classes.js I get the following error: Error reading file "./classes.js": File not found

The error doesn't make much sense, since the file exist. Same error is shown when the absolute file path is used. I'm wondering if there is something wrong with tsc module or am I missing something?

Fenton
  • 241,084
  • 71
  • 387
  • 401
Alex Objelean
  • 3,893
  • 2
  • 27
  • 36

2 Answers2

3

The typescript compiler specifically looks for extensions .str and .ts. Here is the code that resolves input file names:

if(!TypeScript.isSTRFile(normalizedPath) && !TypeScript.isTSFile(normalizedPath)) {
    normalizedPath += ".ts";
}  

The compiler then looks for a file with name normalizedPath, which in your case corresponds to classes.js.ts, which does not exist. In my opinion, the compiler should output a better error message here.

Valentin
  • 7,874
  • 5
  • 33
  • 38
  • I don't see why a tool would care about the extension of the file it should compile. Also, it doesn't output to the console the compiled js when the --out file is not used. – Alex Objelean Oct 06 '12 at 09:06
  • Hmm, I think in most cases that actually makes sense. Consider for example a C compiler which has to deal with `.h` and `.c` files. I just asked the `javac` compiler to compile a file `Test.c` which it refused to do. – Valentin Oct 06 '12 at 09:16
  • The file extension matters because the top-level grammar of a `.d.ts` file is different from the grammar for `.ts`. Arguably the compiler should assume `.ts` given no other information, but as @Valentin notes, it's not really standard behavior for most compilers. – Ryan Cavanaugh Oct 06 '12 at 22:11
0

Apparently the tsc node module works only when the compiled typescript file has ts extension. I believe this is a temporary limitation which could be fixed in future versions of tsc.

Alex Objelean
  • 3,893
  • 2
  • 27
  • 36
  • I am using 0.9.1.1 and it is yet to be fixed. `c:\Temp>tsc --declaration foo.js` gives me the thoroughly misleading: `error TS5007: Cannot resolve referenced file: 'foo.js'.` when `foo.js` is there. – vegemite4me Nov 13 '13 at 13:57