My scenario involves an interface that extends multiple other interfaces from an external library:
interface LabeledProps extends TextProps, ComponentProps {
id: string;
count: number;
...
}
In a different section of the codebase, there is an object of type LabeledProps
and I need to iterate through all its properties:
function myFunc(props: LabeledProps):void {
for (let key in props) {
const val = props[key]; // <-- ERROR IS HERE
// carry out additional tasks
}
}
The line const val = props[key]
triggers a TypeScript error (even though the code still compiles and executes correctly):
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'LabeledProps'.
What is the correct approach (or approaches) to address this issue? Thank you!
(Note: While adding [key: string]: any
to the interface definition would eliminate the error message, I am seeking a genuine resolution rather than just masking the error.)