-1

I am doing something like the below in my TS project:

['components.d.ts', '.eslintrc.js'].forEach(fileName => {
  console.log(fileName)
})

This is valid JS code and TS should infer fileName as string and allow me to loop over it. However, I am getting error saying:

Element implicitly has an 'any' type because expression of type '".eslintrc.js"' can't be used to index type 'void'.
  Property '.eslintrc.js' does not exist on type 'void'

Unsafe call of an `any` typed value. eslint(@typescript-eslint/no-unsafe-call)

Left side of comma operator is unused and has no side effects. ts(2695)

enter image description here

My tsconfig:

{
    "compilerOptions": {

        "target": "ESNext",
        "module": "ESNext",
        "moduleResolution": "node",
        "baseUrl": "./",
        "paths": {
            "@/*": [
                "src/*"
            ],
            "@templates/*": [
                "src/templates/*"
            ],
        },
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "noImplicitAny": true,
        "strictNullChecks": true,
        "strictFunctionTypes": true,
        "strictBindCallApply": true,
        "strictPropertyInitialization": true,
        "noImplicitThis": true,
        "noFallthroughCasesInSwitch": true,
        "noImplicitOverride": true,
        "skipLibCheck": true
    },
    "exclude": [
        "./node_modules",
    ],
    "include": [
        "src/**/*"
    ]
}

Thanks

JD Solanki
  • 898
  • 8
  • 18
  • 2
    Do you have anything in the previous line? You might be missing a `;` – adiga Nov 29 '22 at 06:12
  • I am not seeing errors: https://www.typescriptlang.org/play?noFallthroughCasesInSwitch=true&noImplicitOverride=true#code/Nocgxg9gtgDhB2BTeAXAzgOgCYfSANAAQgaJoA2AlqgE5gYBWaIAuhgGYQ0CiAhmAAsAFO0rlEAOV5REhALwA+QgG8AUIUKR4aCOIzkIAcxFjJ0xAEpVAXytA – Leland Nov 29 '22 at 06:14
  • @adiga I don't use semicolons in my code so there's no issue regarding semicolons – JD Solanki Nov 29 '22 at 06:30

1 Answers1

2

You probably have a method call in the previous line which doesn't end with a ;. Something like this:

console.log()

['components.d.ts', '.eslintrc.js'].forEach(fileName => {
  console.log(fileName)
})

In this case, a semicolon is NOT inserted automatically. So, it ends up being

console.log()['components.d.ts', '.eslintrc.js']

or just

console.log()['.eslintrc.js']

because of the comma operator. This explains the error you are getting:

'.eslintrc.js' does not exist on type 'void'

If you add a ; at the end of the previous line, the error should go way. Here's a link to the TS Payground which reproduces the error


Further reading:

adiga
  • 34,372
  • 9
  • 61
  • 83
  • I don't use semicolons in my code so there's no issue regarding semicolons. Moreover, I am not getting the error "Uncaught TypeError: Cannot read properties of undefined" I am getting a type error as shown in the above screenshot. – JD Solanki Nov 29 '22 at 06:29
  • 1
    @JDSolanki the error occurs BECAUSE you are not using semicolons. The error in the snippet is what javascript would throw if the code is run. The TS playground shows the error you are getting. You need to add a semicolon in the line before it. Otherwise, `[]` will be appended to the previous line's expression. – adiga Nov 29 '22 at 06:31
  • Yes, you are true @adiga. Thanks, I didn't know about it. – JD Solanki Nov 29 '22 at 06:39