- Published on
error TS6059: File is not under 'rootDir' is expected to contain all source filesの解決方法
yarn tscをして下記のようなエラーが出たときの対処方法を紹介します。
$ yarn tsc error TS6059: File '/test/test.ts' is not under 'rootDir' '/src/index.ts'. 'rootDir' is expected to contain all source files. The file is in the program because: Matched by default include pattern '**/*'
前提として、このときのファイル構造は下記のようになっています。
. ├── package.json ├── src │ └── app.ts ├── test │ └── test.ts ├── tsconfig.json └── yarn.lock
また、このときのtsconfig.jsonは以下のようになっています。rootDirを指定しているのでよさそうに見えるのですが、これだとダメで、明示的にどれを入れるのか、外すのかを指定してあげる必要があります。
{ "compilerOptions": { "target": "ES6", "lib": ["ES6"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ "module": "commonjs", /* Specify what module code is generated. */ "rootDir": "src", /* Specify the root folder within your source files. */ "outDir": "./bin", /* Specify an output folder for all emitted files. */ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ /* Type Checking */ "strict": true, /* Enable all strict type-checking options. */ "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ "skipLibCheck": true, /* Skip type checking all .d.ts files. */ }, }
このようにinclude
かexclude
を使うと問題を解決できます
{ "compilerOptions": { "target": "ES6", "lib": ["ES6"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ "module": "commonjs", /* Specify what module code is generated. */ "rootDir": "src", /* Specify the root folder within your source files. */ "outDir": "./bin", /* Specify an output folder for all emitted files. */ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ /* Type Checking */ "strict": true, /* Enable all strict type-checking options. */ "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ "skipLibCheck": true, /* Skip type checking all .d.ts files. */ }, "include": ["src"] }
あるいは、 excludeを使います。今回はtest
以下は入れたくないので、exclude: ['test']
としてやります。
{ "compilerOptions": { "target": "ES6", "lib": ["ES6"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ "module": "commonjs", /* Specify what module code is generated. */ "rootDir": "src", /* Specify the root folder within your source files. */ "outDir": "./bin", /* Specify an output folder for all emitted files. */ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ /* Type Checking */ "strict": true, /* Enable all strict type-checking options. */ "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ "skipLibCheck": true, /* Skip type checking all .d.ts files. */ }, "exclude": ["test"] }
これでエラーが出なくなります。
参考: error TS6059: File 'xxx/node_modules/typescript/Gulpfile.ts' is not under 'rootDir' #10713