tsconfig.json 中的 typescript outDir 设置不起作用

typescript outDir setting in tsconfig.json not working

我似乎无法在 package.json 中使用 outDir 标志。目录结构非常简单:tsconfig.json 在根级别,还有一个 src/ 目录和一个 index.ts 文件以及代表其他模块的其他目录。

当运行 对索引文件执行tsc 命令时,它会在其旁边创建一个新文件,而不是在构建目录中。我做错了什么?

我的 tsconfig:

{
  "compilerOptions": {
    "outDir": "build"
  }
}

我的 npm 构建脚本:

"build": "tsc src/index.ts"

我正在从项目的根目录调用脚本。有趣的是,运行 带有 --outDir 标志的相同脚本工作得很好。

当您使用 tsc src/index.ts 传入文件进行编译时,您的 tsconfig.json 将被忽略。

来自documentation

When input files are specified on the command line, tsconfig.json files are ignored.

您的 npm 构建脚本应该只是 tsc 而不传递任何文件。

这是我的文件夹结构。

将 typescript 文件保存在 src 文件夹中,并将 tsconfig.json 保存在根目录中。

在 tsconfig json 文件中,在 compilerOptions 中为 outDir 添加文件夹名称

"compilerOptions": {    
    "outDir": "build",
    "module": "commonjs",
    "target": "es6",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "noImplicitAny": true,
    "sourceMap": true
  },

和运行下面的命令。

只需 cd 到根文件夹并键入

tsc

tsc --outDir .

这将使用 js 和 map.js 文件构建 outDir 文件夹。

来源:https://github.com/Microsoft/TypeScript/issues/10585

如果您正在使用 incremental 编译器选项,如果您删除/修改了 outDir 中的文件但没有删除 .tsbuildinfo 文件,则可能无法获得输出。

我的问题有点不同,但 Google 把我带到了这里 - 所以我想其他人也可能。

您需要声明您的 tsconfig 文件位置,而不是您要构建的文件。

tsc --build mocks/tsconfig.json

在我的例子中,它被忽略了,因为我在 tsconfig.json 中有 noEmit: true。无论出于何种原因,文件 still 已发出,但在同一目录中而不是在 outDir.

之后

正确读取配置文件,使用标志时也出现此错误。

确保在“compilerOptions”下定义了“outDir”

我将其定义在与“compilerOptions”相同的级别

{
  "compilerOptions": {
    "baseUrl": "node_modules/@types",
    "lib": [
      "es6"
    ],
    "outDir": "dist",
    "esModuleInterop": true,
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "src/**/*"
  ],
}