I'm struggling with defining types for the following code:
type Language = 'en' | 'nl';
interface CacheObject {
[key: string | number | Language]: string;
}
const cache: CacheObject = {};
export const init = (dir: string): Promise<void> =>
fsPromises.readdir(dir).then(files =>
files
.filter(files => files.endsWith('.json'))
.forEach(async file => {
cache[file.slice(0, -5)] = await import(`${dir}/${file}`);
})
);
export const translate = (lang: Language): any => (key: string) =>
key.split('.').reduce((acc, val) => acc[val], cache[lang]);
The issues I'm facing are:
const cache: CacheObject
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'CacheObject'.
No index signature with a parameter of type 'string' was found on type 'CacheObject'.
const cache: CacheObject
Element implicitly has an 'any' type because expression of type 'Language' can't be used to index type 'CacheObject'.
Property 'en' does not exist on type 'CacheObject'.
Can anyone provide guidance on how to address these issues?