【发布时间】: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