将 TS6133(已声明但从未阅读)显示为警告而不是错误
Show TS6133 (declared but never read) as warning instead of error
以前在使用 typescript 处理 React 项目时,有些情况下我会在不读取变量的情况下声明它,它会在控制台上将其作为警告通知。 VSCode 也会通过黄色波浪线通知。
在我正在处理的项目中,未使用的变量显示为 'breaks' 应用程序的错误。我希望将未使用的变量作为警告而不是错误通知。
尝试过的一些事情包括修改 tsconfig.json
。
{
"noUnusedLocals": false,
"noUnusedParameters": false,
}
虽然这会通知我控制台中未使用的变量,但 'break' 应用程序不会,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"]
创建 .env
文件解决了我的问题。这些是我在 .env
中设置的变量
TSC_COMPILE_ON_ERROR=true
ESLINT_NO_DEV_ERRORS=true
中找到更多信息
以前在使用 typescript 处理 React 项目时,有些情况下我会在不读取变量的情况下声明它,它会在控制台上将其作为警告通知。 VSCode 也会通过黄色波浪线通知。
在我正在处理的项目中,未使用的变量显示为 'breaks' 应用程序的错误。我希望将未使用的变量作为警告而不是错误通知。
尝试过的一些事情包括修改 tsconfig.json
。
{
"noUnusedLocals": false,
"noUnusedParameters": false,
}
虽然这会通知我控制台中未使用的变量,但 'break' 应用程序不会,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"]
创建 .env
文件解决了我的问题。这些是我在 .env
TSC_COMPILE_ON_ERROR=true
ESLINT_NO_DEV_ERRORS=true
中找到更多信息