简体   繁体   English

创建 React 应用程序:ESLint 配置不适用于 Typescript

[英]Create React App: ESLint config is not working with Typescript

I have created a react app using CRA (typescript template) and TypeScript is not following rules written in ESLint config.我使用 CRA (打字稿模板)创建了一个反应应用程序,而 TypeScript 没有遵循 ESLint 配置中编写的规则。 This is quite weird for me because I use this config in every react project I setup.这对我来说很奇怪,因为我在我设置的每个 React 项目中都使用了这个配置。 I'm sharing below some information that might be useful.我在下面分享一些可能有用的信息。

.eslintrc .eslintrc

None of the rules written below are being followed by TypeScript. TypeScript 没有遵循下面编写的任何规则。

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["react-hooks"],
  "extends": ["airbnb-typescript", "plugin:@typescript-eslint/recommended"],
  "parserOptions": { "project": "./tsconfig.json" },
  "rules": {
    "react/prop-types": "off",
    "quotes": ["error", "single"],
    "newline-before-return": "error",
    "import/prefer-default-export": "off",
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "error",
    "lines-between-class-members": "error",
    "import/newline-after-import": ["error", { "count": 1 }],
    "comma-dangle": [
      "error",
      {
        "arrays": "always-multiline",
        "objects": "always-multiline",
        "imports": "always-multiline",
        "exports": "always-multiline",
        "functions": "always-multiline"
      }
    ]
  },
  "ignorePatterns": [
    "config",
    "public",
    "scripts",
    "serviceWorker.ts",
    "setupTests,ts"
  ]
}

tsconfig.json配置文件

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}

package.json包.json

{
  "name": "foo",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.13.1",
    "react-dom": "^16.13.1"
  },
  "scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "jest": {
    "roots": [
      "<rootDir>/src"
    ],
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!src/**/*.d.ts"
    ],
    "setupFiles": [
      "react-app-polyfill/jsdom"
    ],
    "setupFilesAfterEnv": [
      "<rootDir>/src/setupTests.ts"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
      "<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
    ],
    "testEnvironment": "jest-environment-jsdom-fourteen",
    "transform": {
      "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
      "^.+\\.module\\.(css|sass|scss)$"
    ],
    "modulePaths": [],
    "moduleNameMapper": {
      "^react-native$": "react-native-web",
      "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "web.ts",
      "ts",
      "web.tsx",
      "tsx",
      "json",
      "web.jsx",
      "jsx",
      "node"
    ],
    "watchPlugins": [
      "jest-watch-typeahead/filename",
      "jest-watch-typeahead/testname"
    ]
  },
  "babel": {
    "presets": [
      "react-app"
    ]
  },
  "devDependencies": {
    "@babel/core": "7.9.0",
    "@svgr/webpack": "4.3.3",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "@types/jest": "^24.9.1",
    "@types/node": "^12.12.62",
    "@types/react": "^16.9.49",
    "@types/react-dom": "^16.9.8",
    "@types/react-router-dom": "^5.1.5",
    "@typescript-eslint/eslint-plugin": "^2.10.0",
    "@typescript-eslint/parser": "^2.10.0",
    "babel-eslint": "10.1.0",
    "babel-jest": "^24.9.0",
    "babel-loader": "8.1.0",
    "babel-plugin-named-asset-import": "^0.3.6",
    "babel-preset-react-app": "^9.1.2",
    "camelcase": "^5.3.1",
    "case-sensitive-paths-webpack-plugin": "2.3.0",
    "css-loader": "3.4.2",
    "dotenv": "8.2.0",
    "dotenv-expand": "5.1.0",
    "eslint": "^6.6.0",
    "eslint-config-react-app": "^5.2.1",
    "eslint-loader": "3.0.3",
    "eslint-plugin-flowtype": "4.6.0",
    "eslint-plugin-import": "2.20.1",
    "eslint-plugin-jsx-a11y": "6.2.3",
    "eslint-plugin-react": "7.19.0",
    "eslint-plugin-react-hooks": "^1.6.1",
    "file-loader": "4.3.0",
    "fs-extra": "^8.1.0",
    "html-webpack-plugin": "4.0.0-beta.11",
    "identity-obj-proxy": "3.0.0",
    "jest": "24.9.0",
    "jest-environment-jsdom-fourteen": "1.0.1",
    "jest-resolve": "24.9.0",
    "jest-watch-typeahead": "0.4.2",
    "mini-css-extract-plugin": "0.9.0",
    "optimize-css-assets-webpack-plugin": "5.0.3",
    "pnp-webpack-plugin": "1.6.4",
    "postcss-flexbugs-fixes": "4.1.0",
    "postcss-loader": "3.0.0",
    "postcss-normalize": "8.0.1",
    "postcss-preset-env": "6.7.0",
    "postcss-safe-parser": "4.0.1",
    "react-app-polyfill": "^1.0.6",
    "react-dev-utils": "^10.2.1",
    "resolve": "1.15.0",
    "resolve-url-loader": "3.1.1",
    "sass-loader": "8.0.2",
    "semver": "6.3.0",
    "style-loader": "0.23.1",
    "terser-webpack-plugin": "2.3.8",
    "ts-pnp": "1.1.6",
    "typescript": "^3.7.5",
    "url-loader": "2.3.0",
    "webpack": "4.42.0",
    "webpack-dev-server": "3.11.0",
    "webpack-manifest-plugin": "2.2.0",
    "workbox-webpack-plugin": "4.3.1"
  }
}

Note: React app has been ejected注意: React 应用已被弹出

Fixed:固定的:

My editor VS Code was missing this in settings.json : "eslint.validate": ["typescript", "typescriptreact"]我的编辑器 VS Code 在settings.json中缺少这个: "eslint.validate": ["typescript", "typescriptreact"]

This is needed to explicitly tell ESLint to watch TypeScript files for linting errors (by default it lints only JavaScript and JSX files).这需要明确告诉 ESLint 监视 TypeScript 文件的 linting 错误(默认情况下,它仅对 JavaScript 和 JSX 文件进行 lint)。

Hope this helps you as well!希望这对你也有帮助! :) :)

These things usually happens when I'm using VSCODE.这些事情通常发生在我使用 VSCODE 时。 Usually there are 3 things I do to Fix.通常我会做 3 件事来修复。 If this config works for you in other projects it might be usefull.如果这个配置在其他项目中对你有用,它可能会很有用。

  1. Run the "Reload Window" in VSCODE, usually fix the problem for me在VSCODE中运行“Reload Window”,通常会为我解决问题
  2. Delete the node_modules and reinstall de deps, I use yarn and it fixed the problem before删除node_modules并重新安装de deps,我使用yarn并解决了之前的问题
  3. It might worth reacreate manually the .eslintrc .可能值得手动重新创建 .eslintrc 。 Maybe there's an update in CRA and things changed.也许 CRA 有更新,事情发生了变化。 Usually I start a new project and run eslint --init and start creating a new eslintrc通常我开始一个新项目并运行 eslint --init 并开始创建一个新的 eslintrc

And it's cool to check if the eslint plugin is workin in your IDE.检查 eslint 插件是否在您的 IDE 中工作很酷。

obs : I woul'd post this as a comment, however I can't yet. obs :我会将此作为评论发布,但我还不能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用 typescript 反应 CRA 不能与 eslint 一起使用 - React CRA with typescript not working with eslint create-react-app 版本 5 的“eslint-config-react-app”问题 - "eslint-config-react-app" issue with create-react-app version 5 关于“create-react-app --template typescript”如何与 typescript-eslint 连接的困惑 - Confusion about how `create-react-app --template typescript` hooked up with typescript-eslint 创建反应应用程序 - 没有打字稿,得到错误:无法加载解析器&#39;@typescript-eslint/parser&#39; - create react app - without typescript , got Error: Failed to load parser '@typescript-eslint/parser' 为什么 create-react-app 在尝试 eslint 时会给出 typescript 错误(尽管没有 typescript 文件)? - Why is create-react-app giving typescript error when trying to eslint (though no typescript files)? ESLint 错误 - ESLint 找不到配置“react-app” - ESLint error - ESLint couldn't find the config "react-app" Atom &amp; eslint:找不到模块 'eslint-config-react-app' - Atom & eslint: Cannot find module 'eslint-config-react-app' 如何配置 eslintrc 并将 React 添加到 eslint(使用 airbnb、typescript)而不创建 react 应用程序 - How to configure eslintrc and add React to eslint (with airbnb, typescript) without create react app 使用typescript工作的create-react-app不起作用 - create-react-app with typescript not working 绝对路径不适用于使用 typescript 创建反应应用程序 - Absolute Path Not working for create react app with typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM