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)
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:
- ES6 Array destructuring weirdness
- What are the rules for JavaScript’s automatic semicolon insertion (ASI)?
