如何解决将 TS6133已声明但从未读取显示为警告而不是错误
以前在使用 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 规则,则指定违规应该是警告,而不是错误。如果需要,请更改您的构建过程,以便只有错误(而非警告)会阻止构建成功。
"@typescript-eslint/no-unused-vars": ["warning"]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。