網站搜尋

typescript - 執行 tsc 命令沒有任何反應,忽略 tsconfig 文件嗎?


我已經在我的專案中加入了 TypeScript,並且建立了 tsconfig.json。我原本還不打算把所有的 .js 檔案改成 .ts 檔案,所以我在 tsconfig 中將 allowJS 設為 true。

但是當我執行 tsc 時,它什麼都不做。tsc -p . 也什麼都不做。tsc --version 是 3.8.3。最終,我決定對單個檔案執行它:

tsc src/index.js
error TS6504: File 'src/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?

所以看起來它根本沒有看到 tsconfig 文件?我做錯了什麼嗎?我是在 Windows 上。

答案#1

是的,tsc 會忽略你的 tsconfig.json,因為你已經傳遞了一個要處理的文件給它。

Typescript 文件:

當在命令列上指定輸入檔案時,tsconfig.json 檔案會被忽略。

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

在你的 tsconfig.json 中使用 "include" 或 "files" 鍵。

{
    "compilerOptions": {
        "allowJs": true,
        "outDir": "dist"

    },
    "files": [
        "src/index.js"
    ]
}

現在運行 tsc,編譯器將會建立一個新的目錄 dist,其中包含你的 index.js 文件。