Chrome Pointer

2023年12月25日 星期一

TypeScript的tsconfig.json無法讀取到之解決方法

 參考文章

💓Docker常用指令介紹


Q:為何tsconfig.json沒有自動創建?

在新建 TypeScript 專案時,tsconfig.json 檔案不會自動產生。這個檔案是用來設定 TypeScript 編譯器的設定檔,通常你需要自行建立。


你可以手動創建 tsconfig.json 檔案,並在裡面設定 TypeScript 編譯器的選項,比如指定編譯後的 JavaScript 檔案輸出目錄、選擇使用的 ECMAScript 版本、設定模組系統等等。這樣做有助於自定義和設置你的 TypeScript 專案。


Q:在TypeScript中,為何tsconfig.json沒有辦法讀取到?


{
  "compilerOptions": {
    /* Projects */

    /* Language and Environment */
    "target": "es2016",                 /* 指定生成的 JavaScript 版本為 ES2016,用於定義所產生的 JavaScript 文件中使用的語言特性和相容的庫聲明。 */

    /* Modules */
    "module": "commonjs",               /* 指定生成的模塊代碼,此處為 commonjs。 */

    /* Emit */
    "esModuleInterop": true,             /* 生成額外的 JavaScript 代碼以便支持導入 CommonJS 模塊,這將啟用 'allowSyntheticDefaultImports' 以保證類型相容性。 */
    "forceConsistentCasingInFileNames": true, /* 確保導入時的大小寫一致性。 */

    /* Type Checking */
    "strict": true,                     /* 啟用所有嚴格的類型檢查選項。 */

    /* Completeness */
    "skipLibCheck": true,               /* 跳過對所有 .d.ts 檔案的類型檢查。 */
    "outDir": "dist"                    /* 指定編譯後的 JavaScript 檔案輸出到 'dist' 資料夾。 */
  },
  "include": [
    "src/*.ts"                         /* 包含的 TypeScript 檔案所在的資料夾和檔案匹配模式。 */
  ]
}



在您的 tsconfig.json 中指定了 include 的目錄和檔案模式,應該將這些符合模式的 TypeScript 檔案列入編譯。然而,當您執行 tsc index.ts 時卻出現了找不到檔案的錯誤。

D:\ts>tsc index.ts
error TS6053: File 'index.ts' not found.
  The file is in the program because:
    Root file specified for compilation

Found 1 error.

假設 index.ts 真的存在於 src 目錄下,您可以嘗試以下幾種方法:

1. 使用 tsc 指令:確保終端機的目錄位置位於 D:\ts,然後執行以下指令:

tsc
2.指定 --project 選項:透過指定 --project 選項來告知 TypeScript 編譯器使用哪個 tsconfig.json 檔案。

tsc --project tsconfig.json

這樣做應該可以成功編譯整個專案。


❤Error:
Duplicate function implementation.ts(2393)

在tsconfig.json加上下面這行,並且重新啟動VS code。
本案重啟VS code後才成功把TS的重複選告錯誤解除。
"skipLibCheck": true,               /* 跳過對所有 .d.ts 檔案的類型檢查。 */



沒有留言:

張貼留言

喜歡我的文章嗎? 喜歡的話可以留言回應我喔! ^^