簡體   English   中英

Yarn 2 Monorepo VSCode 無法根據 tsconfig.json 設置解析 Typescript 類型

[英]Yarn 2 Monorepo VSCode cannot resolve Typescript types based on tsconfig.json settings

我正在使用 Yarn Workspaces 開發 Yarn 2 monorepo,在此過程中,我碰壁試圖讓 VSCode 解析 TypeScript 類型。

我已經按照Yarn 提供的關於讓 VSCode 解析這些類型的信息進行了跟進。

但是,VSCode無法正確解析類型,除非在我的tsconfig.json中,我注釋掉以下兩行:

"include": ["src/**/*"],
"exclude": ["tests/**/*"],

這不是一個問題,因為它可以工作,但是現在當我構建我的 typescript 項目時,我在構建中包含了tests文件夾,這並不理想。

這些設置和 Yarn 的交互方式有什么我遺漏的嗎? 是否有已知的解決方法可以讓 typescript 正確構建並讓 VSCode 正確解析類型?

我的完整tsconfig.json如下:

{
  // "include": ["src/**/*"],
  // "exclude": ["tests/**/*"],
  "compilerOptions": {
    /* Language and Environment */
    "target": "es2016",

    /* Modules */
    "module": "es6",
    "moduleResolution": "node",

    /* Emit */
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "outDir": "dist",

    /* Interop Constraints */
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,

    /* Type Checking */
    "strict": true,
    "skipLibCheck": true,
  }
}

想通了這個問題。

Yarn 使用的 TypeScript SDK 使用tsconfig.json來確定應該應用到哪些文件。 如果您在tsconfig.json中使用includeexclude選項,則 Yarn 使用的 SDK將應用這些選項。 因此,您需要在基本tsconfig.json中注釋掉這些選項。

根據需要讓 TypeScript 包含/排除文件的解決方案是擁有用於構建步驟的特定配置文件。

因此,您將有一個tsconfig.json如下:

{
  "compilerOptions": {
    "target": "es2016",
    "module": "es6",
    "moduleResolution": "node",
    "declaration": true,
    "declarationMap": true,
    "outDir": "dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

您將擁有另一個命名為tsconfig.json的文件,其中包含以下內容:

{
  "extends": "tsconfig.json",
  "exclude": ["./tests/**/*"]
}

該文件基本上可以命名為您想要的任何名稱; 我選擇tsconfig.build.json來明確說明它僅用於構建步驟。

然后,在您的 package 腳本中,您需要使用-p標志調用tsc ,向其提供tsconfig.build.json的路徑,例如tsc -p tsconfig.build.json

使用這種結構,您可以兩全其美; 工作 IntelliSense一個干凈的dist文件夾。

暫無
暫無

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

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