I have a function which takes an object and converts its keys to snake cased strings and returns it. I'm using reduce
to construct the new object but can't seem to resolve this error:
Element implicitly has an 'any' type because type '{}' has no index signature.ts(7017)
Which occurs on the line return { ...o, [snakeCase(k)]: evt[k] };
stating that evt[k]
has the any
type. How do I resolve this? Are index signatures discarded because this is an empty object even though it has a type which has an index signature?
interface StringKeyMap<T> {
readonly [key: string]: T[keyof T];
}
function snakeCaseKeys<T extends object>(evt: T): StringKeyMap<T> {
const initial: StringKeyMap<T> = {};
return Object.keys(evt).reduce<StringKeyMap<T>>((o, k) => {
return { ...o, [snakeCase(k)]: evt[k] };
}, initial);
}