Aye*_*har 5 node.js typescript eslint
.test.ts
过去三天我一直在尝试修复文件的此错误。
我有我tsconfig
的如下
{
"compilerOptions": {
"target": "es6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["ESNext"], /* 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. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
"baseUrl": "src", /* Specify the base directory to resolve non-relative module names. */
"types": ["jest", "node"], /* Specify type package names to be included without being referenced in a source file. */
"resolveJsonModule": true, /* Enable importing .json files */
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
"checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
"outDir": "./dist", /* Specify an output folder for all emitted files. */
"removeComments": true, /* Disable emitting comments. */
"importsNotUsedAsValues": "error", /* Specify emit/checking behavior for imports that are only used for types */
"isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
"allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"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.. */
"noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
"noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
"exactOptionalPropertyTypes": false, /* Interpret optional property types as written, rather than adding 'undefined'. */
"noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
"noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
"noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
"noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
"noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
"allowUnusedLabels": false, /* Disable error reporting for unused labels. */
"allowUnreachableCode": false, /* Disable error reporting for unreachable code. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": ["src"],
"exclude": ["node_modules", "dist", "coverage", "src/__tests__", "**/*.test.ts"]
}
Run Code Online (Sandbox Code Playgroud)
我的eslintrc.js
有这些内容
module.exports = {
env: {
es2021: true,
node: true,
jest: true,
},
extends: [
'eslint:recommended',
'airbnb-base',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'plugin:prettier/recommended',
'plugin:import/typescript',
'prettier'
],
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
project: 'tsconfig.json',
tsconfigRootDir: './',
},
plugins: ['@typescript-eslint', 'prettier', 'import'],
rules: {
}
}
Run Code Online (Sandbox Code Playgroud)
我收到src/__tests__/sample.test.ts
这个错误
Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: src/__tests__/sample.test.ts.
The file must be included in at least one of the projects provided.
Run Code Online (Sandbox Code Playgroud)
我注意到它只发生在.test.ts
文件中,其余仅具有扩展名的打字稿文件.ts
工作正常
我缺少什么?
因此,由于我排除了 中的测试文件,因此tsconfig,json
我必须在开发过程中以某种方式将 eslint 的这些文件添加到 lint 中,并在构建时排除它们。
我最终创建了一个tsconfig.eslint.json
包含内容的新文件
{
"extends": "./tsconfig.json", <-- since the entire src folder is already included here
"exclude": ["node_modules", "dist", "coverage"] <--- override the exclude property to not have test files here
}
Run Code Online (Sandbox Code Playgroud)
而在我的eslintrc.js
parserOptions: {
sourceType: 'module',
project: 'tsconfig.eslint.json',
tsconfigRootDir: './',
},
Run Code Online (Sandbox Code Playgroud)
这修复了错误。
归档时间: |
|
查看次数: |
17001 次 |
最近记录: |