I'm struggling to figure out a solution for the error message that keeps popping up, indicating that the forEach
method is not accessible in the code snippet below.
type Foo = Array<string> | string;
type FooCollection = { [key: string]: Foo }
const bar = (data: FooCollection) => {
Object.keys(data)
.forEach((key) => {
if (Array.isArray(data[key])) {
// this forEach is disputed
data[key].forEach((v, i) => {
console.log(v, i);
});
}
});
};
The following code, however, runs without any errors:
const baz = (value: Foo) => {
if (Array.isArray(value)) {
value.forEach((v, i) => {
console.log(v, i);
});
}
};
After some investigation, I discovered a potential workaround but I am unsure about its effectiveness. Here's the modified snippet:
const qux = (data: FooCollection) => {
Object.keys(data)
.forEach((key) => {
if (Array.isArray(data[key])) {
baz(data[key]);
} else {
// ...
}
});
};