Search code examples
reactjstypescripttsconfigtypescript-eslint

Show TS6133 (declared but never read) as warning instead of error


Previously when working on react projects using typescript, there are instances where I would declare a variable without reading it and it would notify about it as a warning on console. VSCode would also notify via a yellow squiggly line.

On the project I am working on, Unused variables are displayed as errors which 'breaks' the app. I would like that unused variables be notified as a warning and not an error.

Some of the things tried include modifying tsconfig.json.

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

While this notifies me of unused variables in the console, and doesn't 'break' the app, VSCode doesn't notify of unused variables.

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"
  }
}

Error Message:

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>;

Solution

  • Creating a .env file solved the issue for me. These are the variables I set in the .env

    TSC_COMPILE_ON_ERROR=true
    ESLINT_NO_DEV_ERRORS=true
    

    More info can be found in cra's documentation