Dan*_*Dan 109 typescript eslint typescript-eslint
我创建了一个新的 React Native 项目 --template typescript
我删除了template
作为样板文件一部分的目录。
然后我继续添加 ESLint:
module.exports = {
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: ["airbnb-typescript-prettier"]
};
Run Code Online (Sandbox Code Playgroud)
但是,当我打开时babel.config.js
,出现此错误
解析错误:已为@typescript-eslint/parser 设置了“parserOptions.project”。
该文件与您的项目配置不匹配:
/Users/Dan/site/babel.config.js
.该文件必须至少包含在提供的项目之一中。eslint
Raf*_*res 71
出现此问题的原因之一如下:
parserOptions.project
;的规则。parserOptions.project
,未指定createDefaultProgram
(它将在未来版本中删除),并且您正在整理项目中未包含的文件(例如babel.config.js
,metro.config.js
)来自TypeScript ESLint Parser 文档:
parserOptions.project
此选项允许您提供项目的
tsconfig.json
. 如果要使用需要类型信息的规则,则需要此设置。(……)
请注意,如果此设置已指定
createDefaultProgram
但未指定,则您只能按照提供的tsconfig.json
文件定义包含在项目中的 lint文件。如果您现有的配置不包括您想要 lint 的所有文件,您可以创建一个单独的tsconfig.eslint.json
.
要解决它,请将您的 ESLint 配置更新为以下内容:
{
// ...
overrides: [
{
files: ['*.ts', '*.tsx'], // Your TypeScript files extension
parserOptions: {
project: ['./tsconfig.json'], // Specify it only for TypeScript files
},
}
],
parser: '@typescript-eslint/parser',
// ...
}
Run Code Online (Sandbox Code Playgroud)
Žel*_*vić 65
您可以创建一个单独的 TypeScript 配置文件 ( tsconfig.eslint.json
) 用于eslint
配置。该文件扩展了必须检查的文件的tsconfig
配置和设置include
密钥。
.eslint.js
:
// ...
parserOptions: {
// ...
project: "./tsconfig.eslint.json",
// ...
},
// ...
Run Code Online (Sandbox Code Playgroud)
tsconfig.eslint.json
:
{
"extends": "./tsconfig.json",
"include": [
// ...
"babel.config.js"
]
}
Run Code Online (Sandbox Code Playgroud)
或者如果你想忽略它,你可以把它放入.eslintignore
.
.eslintignore
:
// ...
babel.config.js
Run Code Online (Sandbox Code Playgroud)
eri*_*ock 21
您需要将该文件添加到您的 tsconfiginclude
数组中。有关更多详细信息,请参阅typescript-eslint/typescript-eslint#967。
Joh*_*edy 18
在我的 ESLint 配置中,我有以下配置:
.eslintrc.js
module.exports = {
root: true,
env: {
node: true,
},
globals: {
Promise: "readonly"
},
parser: "@typescript-eslint/parser",
parserOptions: {
sourceType: "module",
tsconfigRootDir: __dirname,
project: ["./tsconfig.eslint.json"],
},
plugins: ["@typescript-eslint"],
extends: [
"eslint:recommended",
"prettier/@typescript-eslint",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
],
}
Run Code Online (Sandbox Code Playgroud)
更新 修复的关键部分是这样的:
parser: "@typescript-eslint/parser"
Run Code Online (Sandbox Code Playgroud)
和这个:
parserOptions: {
sourceType: "module",
tsconfigRootDir: __dirname,
project: ["./tsconfig.eslint.json"], // could be tsconfig.json too
}
Run Code Online (Sandbox Code Playgroud)
不要忘记将该文件或文件的目录包含在
include
tsconfig.json 数组中。
Seb*_*opp 15
只需将eslint
选项添加ignorePatterns
到您的.eslintrc.js
:
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.json',
},
ignorePatterns: ["babel.config.js"],
...
Run Code Online (Sandbox Code Playgroud)
由于检查项目配置文件没有太大价值,因此您可以安全地忽略它们。
另外,我不会将它们作为您的一部分tsconfig.json
或创建一个特殊tsconfig.eslint.json
文件,只是为了检查目的。
qin*_*127 13
在“.eslintignore”中添加一行:
.eslintrc.js
Jer*_*yal 12
跑 npm i -D @types/node
包括对*.config.js
文件的打字稿支持:
tsconfig.json
:
{
"include": [
"**/*.config.js" // for *.config.js files
]
}
Run Code Online (Sandbox Code Playgroud)
然后重新加载您的 IDE!
adi*_*1ya 11
只需在 .eslintrc.js 文件中添加以下代码即可。
ignorePatterns: ['.eslintrc.js']
Run Code Online (Sandbox Code Playgroud)
该错误意味着存在一个可以编译的文件,但不属于当前定义的项目结构。有几种解决方案:
.eslintignore
或者
.eslintrc.js
文件ignorePatterns: ["babel.config.js"]
// or
ignorePatterns: ["**/*.config.js"]
Run Code Online (Sandbox Code Playgroud)
将文件、文件夹或模式添加到tsconfig.json
文件
include": [
"src",
"other_src",
]
Run Code Online (Sandbox Code Playgroud)
注意一些更改需要重启IDE才能生效
'@typescript-eslint/prefer-nullish-coalescing': 'error'
添加到时我遇到了同样的问题.eslintrc.cjs
,以下设置对我有用:
// tsconfig.json
{
// ...
"include": ["**/*", "**/.*"],
"exclude": ["node_modules", "dist"]
}
Run Code Online (Sandbox Code Playgroud)
// tsconfig.json
{
// ...
"include": ["**/*", "**/.*"],
"exclude": ["node_modules", "dist"]
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
90742 次 |
最近记录: |