Typescript 项目中未引用的.ts文件';s根“;不在根目录下;

Typescript 项目中未引用的.ts文件';s根“;不在根目录下;,typescript,Typescript,我在项目的根目录中有一个松散的实用程序脚本name.ts。需要时,我使用ts节点调用它。此脚本未在实际源中的任何位置引用 我的程序的实际源代码在src下 每当我尝试运行tsc(在tsconfig.json中,rootDir设置为/src),它就会抛出: error TS6059: File '/home/rijndael/projects/mc/js/lua/generate.ts' is not under 'rootDir' '/home/rijndael/projects/mc/js/lu

我在项目的根目录中有一个松散的实用程序脚本
name.ts
。需要时,我使用
ts节点
调用它。此脚本未在实际源中的任何位置引用

我的程序的实际源代码在
src

每当我尝试运行
tsc
(在
tsconfig.json
中,
rootDir
设置为
/src
),它就会抛出:

error TS6059: File '/home/rijndael/projects/mc/js/lua/generate.ts' is not under 'rootDir' '/home/rijndael/projects/mc/js/lua/scripts-ts'. 'rootDir' is expected to contain all source files.
为什么会这样?

正如您在中看到的,
rootDir
仅在使用
outDir
时用于目录结构。您需要的是分别使用
include
exclude
config选项来告诉TypeScript要包括(或排除)哪些文件

在您的情况下,您可以添加
include:['src/***']
以仅使用源目录中的文件,或者使用
exclude:['utilities/***']
(将“utilities”替换为脚本文件夹)排除脚本

Tconfig的示例可能如下所示:

{
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    // your compile options
  },
  "include": ["src/**/*"], // only include your src directory
  "exclude": ["**/*.spec.ts"] // exclude tests and add scripts if you have any in "src/"
}

include
像白名单一样工作。构建中只包含与此处任何全局模式匹配的文件<代码>排除
的工作原理类似于黑名单。与此处任何全局模式匹配的文件都将从您的构建中排除(即使与
include
匹配)。

我同时拥有
rootDir
outDir
。不过,答案不错,我已经用
exclude
解决了这个问题。很高兴它对您有所帮助:)顺便说一句。
rootDir
不会告诉TypeScript要在您的构建中包含哪些文件(即使使用
outDir
)。如果您使用
outDir
,它只会告诉它文件夹结构应该是什么样子,否则没有任何效果。