首页 > 解决方案 > 我可以将 tsconfig 文件放在一个项目中并在另一个项目中使用它吗?

问题描述

我的解决方案项目中有子模块。他们两个(第一个第二个项目)都有 .ts 文件,所以我想把 tsconfig.json 放在子模块中并为他们两个共享它。

在文件系统中,它们的位置如下:

~/solution
--project1
  --Scripts
    --(ts files)
--submodule
  --tsconfig.json(I want to put it here)

~/solution
--project2
  --Scripts
    --(ts files)
--submodule

project1's name is equal with project2's name

我试图将 rootDir 添加到 compilerOptions

   "rootDir": "..\\project1\\Scripts\\**\\*"

我试图添加

  "include": [
    "..\\project1\\Scripts\\**\\*"
  ]

它们都不适合我。

这是我的 tsconfig.json

{
  "compileOnSave": true,
  "buildOnSave": true,
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": true,
    "sourceMap": false,
    "target": "es5",
    "module": "none",
    "types": [],
    "emitBOM": false,
    "charset": "utf8"
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

标签: typescripttsconfig

解决方案


如果你想在两个项目之间共享一个 tsconfig,你可以扩展一个tsconfig.json文件。例如,您有一个共享文件shared.json。每个项目都有自己的tsconfig.json文件并扩展基本文件shared.json,如下所示:

{
  "extends": "../shared",
  <options here that are different>
}

此页面上的更多信息: https ://www.typescriptlang.org/docs/handbook/tsconfig-json.html


推荐阅读