【问题标题】:typescript - Includes types in paths, but not compile themtypescript - 在路径中包含类型,但不编译它们
【发布时间】:2019-08-04 07:59:56
【问题描述】:

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

.
+-- @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

我不知道如何从编译中排除这个目录。

如果有人有想法,谢谢。

【问题讨论】:

    标签: typescript typescript-typings


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 如果我这样做了,我得到了错误Cannot find type definition file for 'common'.。 VSCode 还抱怨找不到类型
    • 您可以使用模块在您的公共文件中导入类。在你的declaration.tsexport class Declaration { … }。在你的utils.tsimport * as Declaration1 from '../@types/declaration1。您现在可以使用Declaration1.Declaration 访问您的班级/类型。也不再需要在您的tsconfig.json 中包含typeRoots
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 2018-04-28
        • 1970-01-01
        • 2019-05-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多