TypeScript is having a hard time keeping track of the discriminated type of Test[string] after the check.
TypeScript can do this with local variables:
if (Array.isArray(someObj)) { someObj.map(() => /*...*/ }
Or explicit properties:
if (Array.isArray(someObj.myArr)) { someObj.myArr.map(() => /*...*/ }
But when the index type is something generic like string, it doesn't quite have enough info for the compiler to figure this inference out.
This has been discussed by the TypeScript team if you want to read more about this. The issue is currently open. Maybe it will be supported in the future, as it does look like the compiler should be able to figure this one out, in theory. (Thanks for the layup @jcalz)
The simplest solution is to change your code into one of the above forms. I would suggest storing a reference to the property value in a local variable and then testing/using that. This allows TypeScript to track type narrowing on that variable.
Object.keys(test).map(key => {
const maybeArray = test[key]
if(Array.isArray(maybeArray)) {
maybeArray.map(i => i)
}
})
Playground