When working with TypeScript, I encountered an issue while trying to import and iterate over all modules from a file. The compiler throws an error at build time. Can anyone help me figure out the correct settings or syntax to resolve this?
import * as dependencies from './dependencies';
for (const key in dependencies) {
console.log(dependencies[key]());
}
The compiler produces the following error during build:
error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof import("/src/dependencies/index")'.
No index signature with a parameter of type 'string' was found on type 'typeof import("/src/dependencies/index")'.
tsconfig.json
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"jsx": "react",
...
... [configuration details]
...
"isolatedModules": true
},
"exclude": ["node_modules"],
"include": ["**/*.ts"]
}
EDIT:
To work around this issue, I'm implementing the following workaround which seems redundant.
import * as dependencies from './dependencies';
interface Collection {
[key: string]: any;
}
const dependencyCollection: Collection = dependencies;
for (const key in dependencyCollection) {
console.log(dependencyCollection[key]());
}