【问题标题】:Yarn 2 Monorepo VSCode cannot resolve Typescript types based on tsconfig.json settingsYarn 2 Monorepo VSCode 无法根据 tsconfig.json 设置解析 Typescript 类型
【发布时间】:2021-12-29 03:31:58
【问题描述】:

我正在使用 Yarn Workspaces 开发 Yarn 2 monorepo,在此过程中,我在尝试让 VSCode 解析 TypeScript 类型时遇到了困难。

我已经按照provided by Yarn 的信息了解如何让 VSCode 解析这些类型。

然而,VSCode正确解析类型除非,在我的tsconfig.json中,我注释掉以下两行:

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

这不是一个 巨大 问题,因为它可以工作,但是现在当我构建我的打字稿项目时,我在构建中包含了 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,
  }
}

【问题讨论】:

    标签: typescript visual-studio-code jestjs yarnpkg yarn-workspaces


    【解决方案1】:

    找出问题所在。

    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 是为了明确说明它仅用于构建步骤。

    然后,在您的包脚本中,您需要使用-p 标志调用tsc,您可以在其中提供tsconfig.build.json 的路径,例如tsc -p tsconfig.build.json.

    通过这种结构,您可以两全其美;工作 IntelliSense一个干净的 dist 文件夹。

    【讨论】:

      猜你喜欢
      • 2019-06-22
      • 2023-02-15
      • 2021-11-01
      • 2019-08-01
      • 2021-02-20
      • 1970-01-01
      • 2018-12-17
      • 1970-01-01
      • 2020-11-30
      相关资源
      最近更新 更多