Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use forEach on inline array when using typescript?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading