【问题标题】:Show TS6133 (declared but never read) as warning instead of error将 TS6133(已声明但从未读取)显示为警告而不是错误
【发布时间】:2021-05-28 09:27:29
【问题描述】:

以前在使用 typescript 处理 react 项目时,在某些情况下我会声明一个变量而不读取它,它会在控制台上通知它作为警告。 VSCode 也会通过黄色波浪线通知。

在我正在处理的项目中,未使用的变量显示为“破坏”应用程序的错误。我希望将未使用的变量作为警告而不是错误通知。

尝试的一些事情包括修改tsconfig.json

{
    "noUnusedLocals": false,
    "noUnusedParameters": false,
}

虽然这会通知我控制台中未使用的变量,并且不会“破坏”应用程序,但 VSCode 不会通知未使用的变量。

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true
  },
  "include": ["src"]
}

.eslintrc.json

{
  "root": true,
  "extends": ["react-app", "react-app/jest"],
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],
  "rules": {
    "@typescript-eslint/no-unused-vars": ["warning"]
  }
}

package.json

{
  "name": "project",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^4.11.3",
    "@material-ui/icons": "^4.11.2",
    "@material-ui/styles": "^4.11.3",
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "material-ui-image": "^3.3.1",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.3",
    "typescript": "^4.1.2",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "lint": "eslint . --ext .js,.jsx,.ts,.tsx --max-warnings 0"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@types/jest": "^26.0.20",
    "@types/node": "^14.14.31",
    "@types/react": "^17.0.2",
    "@types/react-dom": "^17.0.1",
    "@types/react-router-dom": "^5.1.7",
    "@typescript-eslint/eslint-plugin": "^4.15.2",
    "@typescript-eslint/parser": "^4.15.2",
    "eslint": "^7.20.0",
    "prettier": "2.2.1"
  }
}

错误信息:

D:/path/to/project/src/components/ThemeProvider.tsx
TypeScript error in D:/path/to/project/src/components/ThemeProvider.tsx(23,7):
'someVariable' is declared but its value is never read.  TS6133

    21 |   },
    22 | });
  > 23 | const someVariable = {};
       |       ^
    24 |
    25 | const ThemeProvider = ({ children }: { children: ReactNode }) => {
    26 |   return <MUIThemeProvider {...{ theme }}>{children}</MUIThemeProvider>;

【问题讨论】:

  • 我会启用 ESLint 规则以禁止未使用的变量并使用 ESLint 的 VSCode 插件
  • 启用 ESLint 规则不允许在存在未使用变量的情况下进行构建。我正在寻找一种在 vscode 和控制台上显示警告的方法。

标签: reactjs typescript tsconfig typescript-eslint


【解决方案1】:

如果您启用 ESLint 规则,您指定违规应该是一个警告,而不是一个错误。如果需要,更改您的构建过程,以便只有错误(而不是警告)会阻止构建成功。

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-vars.md

"@typescript-eslint/no-unused-vars": ["warning"]

【讨论】:

  • 我在rules 中添加了这个.eslintrc。应用仍然无法编译。
  • 你如何解释 ESLint 的结果?您需要对其进行更改,以便只有错误会导致进程停止,而不是警告。
  • 我已经添加了我的配置详细信息
  • 也将"@typescript-eslint/no-unused-vars": ["warning"] 结果添加到.eslintrc.json: Configuration for rule "@typescript-eslint/no-unused-vars" is invalid: Severity should be one of the following: 0 = off, 1 = warn, 2 = error (you passed '"warning"').
  • 看起来您使用的设置与文档中描述的略有不同 - 因此,要将其设置为警告而不是错误,请使用 1 而不是 ["warning"],听起来像?
【解决方案2】:

创建一个.env 文件为我解决了这个问题。这些是我在.env中设置的变量

TSC_COMPILE_ON_ERROR=true
ESLINT_NO_DEV_ERRORS=true

更多信息可以在cra's documentation找到

【讨论】:

    猜你喜欢
    • 2020-01-08
    • 2019-08-24
    • 2017-11-17
    • 2021-02-25
    • 2021-11-24
    • 1970-01-01
    • 2023-03-14
    • 2011-06-22
    • 1970-01-01
    相关资源
    最近更新 更多