Apologies for adding yet another question of this nature, but despite finding similar ones, I am unable to apply their solutions to my specific case.
Could someone please assist me in resolving this TypeScript error?
The element implicitly has an 'any' type because the expression of type 'string' cannot be used to index the type 'Record<RouteName, TranslatableRoute>'. No index signature with a parameter of type 'string' was found on type 'Record<RouteName, TranslatableRoute>' (7053).
I am seeking the correct approach to fix this issue rather than resorting to workarounds that compromise type safety. Here is a simple example where I encountered the problem.
type RouteName = 'home' | 'account'
interface TranslatableRoute {
cs: string;
en: string;
}
const translatableRoutes: Record<RouteName, TranslatableRoute> = {
home: {
cs: '/',
en: '/'
},
account: {
cs: '/ucet',
en: '/account'
}
}
const findRoute = '/ucet'
const findLang = 'cs'
for (const key in translatableRoutes) {
if (translatableRoutes[key][findLang] === findRoute) {
console.log(`Found route\'s name is "${key}"!`)
}
}
TypeScript playground link.