打字稿-在路径中包含类型,但不进行编译

时间:2019-03-13 15:17:29

标签: typescript typescript-typings

我在打字稿中遇到问题。我具有以下层次结构:

.
+-- @types
|   +-- declaration1.ts
|   +-- declaration2.ts
+-- common
|   +-- utils1.ts
|   +-- utils2.ts
+-- tests
|   +-- utils1.test.ts
|   +-- utils2.test.ts

在我的tsconfig.json中,我将此配置设置为在测试和公用程序(或vscode抱怨)中都暴露出所有类型:

{
  "compilerOptions": {
    "outDir": "build/",
  },
  "include": ["tests", "common", "@types"]
}

我的问题是,我不想编译所有这些东西,我只想编译公用(公用程序,我无法获得单个(甚至多个)入口点)。

所以我想输入tsc并像这样填充build文件夹:

.
+-- build
|   +-- common
|   |   +-- utils1.js
|   |   +-- utils2.js
// nothing else

相反,我得到了:

.
+-- build
|   +-- @types
|   |   +-- declaration1.js
|   |   +-- declaration2.js
|   +-- common
|   |   +-- utils1.js
|   |   +-- utils2.js
|   +-- tests
|   |   +-- utils1.test.js
|   |   +-- utils2.test.js

我无法了解如何从编译中排除此目录。

感谢任何人有想法。

3 个答案:

答案 0 :(得分:0)

要包括但不编译类型,请将其添加到"typeRoots"中的tsconfig.json中。 "include"中的所有内容都会被编译。

{
  "compilerOptions": {
    "typeRoots": [ "@types", "tests" ]
  }
}

要忽略文件或目录,请使用"exclude"选项,例如:

{
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

答案 1 :(得分:0)

您可以在不想构建的文件夹下创建一个新的tsconfig.json,在其中添加'noEmit:true'。

答案 2 :(得分:0)

尝试在您不想构建的文件夹下创建新的tsconfig.json并添加到以下位置:

{
  "compilerOptions": {
    "noEmit": true
  },
  "exclude": ["src/**/*.ts"]     // files you want to build
}

并包含要在原始tsconfig.json中构建的文件:

{
  "compilerOptions": {
    ...
  },
  "include": ["src/**/*.ts"]     // files you want to build
}