tsc not reporting errors when compiling

2.2k Views Asked by At

I'm having some issues figuring out a mysterious situation

I have a tsconfig file:

{
  "compilerOptions": {
    "outDir": "./built",
    "allowJs": true,
    "target": "es5"
  },
  "include": [
    "./app/**/*.ts"
  ]
}

And when I do tsc with the code below, which is clearly wrong:

const credentials = Config.blahblah
import Config from '../../../config'

I know it's wrong because Config is not imported before using it but after.

If i switch the two lines, then the code passes my tests. But the problem is, if I keep the order as above (which should give me an error), and when I do

mocha --compilers ts:ts-node/register,tsx:ts-node/register app/**/index-test.ts

or even

tsc

For instance:

➜  GhostFaceRestful git:(exie/workon_typescript) ✗ mocha --compilers ts:ts-node/register,tsx:ts-node/register app/**/index-test.ts
➜  GhostFaceRestful git:(exie/workon_typescript) ✗

There's no error message at all. This makes debugging extremely difficult. I'm wondering what am I doing wrong? At least tsc should tell me there's an compiling error in this case?

1

There are 1 best solutions below

2
On

That's because it's not a TypeScript error, as ES6 (and, I suppose, TypeScript) import statements are hoisted. (You can verify this by running tsc - it, too, reports no error.)

Interestingly, when it's compiled, the require calls are not hoisted. So this:

const credentials = Config.blahblah
import Config from '../../../config'

becomes this:

"use strict";
var credentials = Config.blahblah;
var Config = require('../../../config');

and you should see a runtime error:

TypeError: Cannot read property 'Config' of undefined

If you want to verify that ts-node is reporting TypeScript compilation errors, use an obvious TypeScript error; something like this:

const n: number = "not-a-number";