1

I am sure that this is something that was already asked multiple times, but I simply cannot get the error related to type "any" with typescript-eslint I have some sample code in src\index.ts

const testArray = [1, 2, 3];
const testString = JSON.stringify(testArray);
const test_Array2 = JSON.parse(testString);

console.log(test_Array2.length);

I have configured the .eslintrc according to my best knowledge

{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "tsconfigRootDir": "./",
    "project": ["./tsconfig.json"]
  },
  "plugins": ["@typescript-eslint"],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking"
  ],
  "rules": {},
  "env": {
    "browser": true,
    "node": true
  }
}

I have a very minimalistic package.json

{
  "name": "typescript-starter",
  "version": "1.0.0",
  "description": "test.",
  "main": "index.js",
  "scripts": {
    "lint": "eslint . --ext .ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^12.7.2",
    "@typescript-eslint/eslint-plugin": "^2.34.0",
    "@typescript-eslint/parser": "^2.34.0",
    "eslint": "^6.8.0",
    "rimraf": "^3.0.0",
    "typescript": "^4.3.5"
  },
  "dependencies": {}
}

and very simple tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
    "lib": ["es6"],
    "allowJs": true,
    "outDir": "build",
    "rootDir": "src",
    "strict": true,
    "noImplicitAny": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "types": ["node"],
    "typeRoots": ["./node_modules/@types"]
  },
  "include": ["src/**/*"]
}

My expectation is that the linter should complain about me using test_Array2.length since test_Array2 has type any (and implicit usage of type any is prohibited in the @typescript-eslint/recommended). Instead the only errors I get are related to camelcase (which proves that my config is actually checking SOMETHING)

  3:7   error  Identifier 'test_Array2' is not in camel case  @typescript-eslint/camelcase
  5:13  error  Identifier 'test_Array2' is not in camel case  @typescript-eslint/camelcase

I am using @typescript-eslint/eslint-plugin@2.34.0 and @typescript-eslint/parser@2.34.0 (basically the most recent version)

Can somebody bring some light onto what I should change to actually get the error from linter?

Dmitry Yudin
  • 45
  • 1
  • 6
  • The thing is, that's not an *implicit* use of `any`. `JSON.parse` explicitly returns `any`. If it were implicit, TypeScript itself would warn you about it (since you have `"noImplicitAny": true` in your `tsconfig.json`). For what it's worth, I avoid this by ensuring any use of `JSON.parse` is in a small set of project-specific utility functions that return a correct type (and validate it before returning, in dev). Sadly that doesn't answer your question. :-D – T.J. Crowder Aug 22 '21 at 11:06
  • I have found this thread https://stackoverflow.com/questions/54064649/how-to-block-implicitly-casting-from-any-to-a-stronger-type but here they are talking about TSlint, which was deprecated. My understanding is that this case should be covered in https://www.npmjs.com/package/@typescript-eslint/eslint-plugin @typescript-eslint/no-unsafe-call rule(which is marked as recommended), but somehow it is not – Dmitry Yudin Aug 22 '21 at 11:23

0 Answers0