当我尝试启用 ESLint 时,我的 React 应用程序遇到错误。我正在努力寻找原因,但还没有任何结果。有谁知道这个问题的原因可能是什么?这是我的 ESLint 配置文件。
module.exports = {
root: true,
env: {
browser: true,
es2020: true,
},
globals: {
Types: 'readonly',
Utils: 'readonly',
React: 'readonly',
JSX: 'readonly',
},
extends: [
'next',
'turbo',
'airbnb',
'eslint:recommended',
'plugin:react/recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'prettier',
'plugin:prettier/recommended',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
sourceType: 'module',
project: './tsconfig.json',
tsconfigRootDir: __dirname,
},
plugins: ['@typescript-eslint', 'simple-import-sort', 'prettier'],
rules: {
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-floating-promises': 'off',
'@typescript-eslint/no-namespace': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/unbound-method': 'off',
'@typescript-eslint/no-misused-promises': 'off',
'@typescript-eslint/no-unused-vars': [
2,
{
argsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
varsIgnorePattern: '^_',
},
],
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/require-await': 'off',
'@typescript-eslint/no-shadow': 'off',
camelcase: 'off',
'import/extensions': 'off',
'import/first': 'error',
'import/newline-after-import': 'error',
'import/no-duplicates': 'error',
'import/no-extraneous-dependencies': 'off',
'import/no-unresolved': 'off',
'import/prefer-default-export': 'off',
'no-console': [
'error',
{
allow: ['warn', 'error'],
},
],
'no-param-reassign': 0,
'no-unused-expressions': 'off',
'no-use-before-define': 'off',
'no-return-assign': 'off',
'no-shadow': 'off',
'prettier/prettier': [
'error',
{
endOfLine: 'auto',
semi: false,
},
],
'react/destructuring-assignment': 'off',
'react/jsx-filename-extension': [
1,
{
extensions: ['.tsx', '.jsx'],
},
],
'react/require-default-props': 'off',
'react/react-in-jsx-scope': 'off',
'react/jsx-props-no-spreading': 'off',
'react/state-in-constructor': 'off',
'simple-import-sort/exports': 'error',
'simple-import-sort/imports': 'error',
'consistent-return': 'off',
// This is bug of eslint with tslint, which always raise the error
'jsx-a11y/label-has-associated-control': 'off',
},
settings: {
react: {
version: 'detect',
},
},
}
我收到错误:无法读取未定义的属性(读取“getTokens”)
为了帮助解决此问题,您可以将 ESLint 及其相关插件更新到最新版本。以下是使用 npm 的方法:
npm install eslint@latest @typescript-eslint/parser@latest @typescript-eslint/eslint-plugin@latest --save-dev
更新依赖项后,请考虑使用简化的 ESLint 配置来启动。这是一个干净的配置,您可以将其用作起点:
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
project: './tsconfig.json',
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react/recommended',
'plugin:prettier/recommended',
],
plugins: ['@typescript-eslint', 'react', 'prettier'],
env: {
browser: true,
es2021: true,
node: true,
},
settings: {
react: {
version: 'detect',
},
},
rules: {
'@typescript-eslint/explicit-module-boundary-types': 'off',
'react/prop-types': 'off',
'prettier/prettier': 'error',
},
};
更新 ESLint 配置后,尝试再次运行 ESLint:
npx eslint --ext .ts,.tsx .
这应该有助于解决 getTokens 错误。如果您仍然遇到问题,请确保您的 tsconfig.json 配置正确并且与 ESLint 设置兼容。