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?
That's because it's not a TypeScript error, as ES6 (and, I suppose, TypeScript)
import
statements are hoisted. (You can verify this by runningtsc
- it, too, reports no error.)Interestingly, when it's compiled, the
require
calls are not hoisted. So this:becomes this:
and you should see a runtime error:
If you want to verify that
ts-node
is reporting TypeScript compilation errors, use an obvious TypeScript error; something like this: