簡體   English   中英

tsc 編譯時不報錯

[英]tsc not reporting errors when compiling

我在弄清楚一個神秘的情況時遇到了一些問題

我有一個 tsconfig 文件:

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

當我使用下面的代碼執行 tsc 時,這顯然是錯誤的:

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

我知道這是錯誤的,因為 Config 不是在使用之前而是在使用之后導入的。

如果我切換兩行,則代碼通過了我的測試。 但問題是,如果我保持上述順序(這應該給我一個錯誤),當我這樣做時

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

甚至

tsc

例如:

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

根本沒有錯誤信息。 這使得調試極其困難。 我想知道我做錯了什么? 至少 tsc 應該告訴我在這種情況下存在編譯錯誤?

那是因為它不是 TypeScript 錯誤,因為 ES6(以及我想是 TypeScript) import語句被提升了。 (您可以通過運行tsc來驗證這一點 - 它也不會報告錯誤。)

有趣的是,當它被編譯時, require調用並沒有被提升。 所以這:

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

變成這樣:

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

你應該會看到一個運行時錯誤:

TypeError: Cannot read property 'Config' of undefined

如果要驗證ts-node是否報告 TypeScript 編譯錯誤,請使用明顯的 TypeScript 錯誤; 像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM