'Using .forEach() after checking if variable is Array type

I have a generic functional component in which I got the following code:

if(Array.isArray(entry[key as keyof T]) {
    entry[key as keyof T].forEach((item: T) => { ... });
}

Where key is a variable string. The .forEach is red underlined with the following error message:

Property 'forEach' does not exist on type 'T[keyof T]'

even though I checked if the value is an array in the conditional statement. How do I work around this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source