The code below would throw error when used with Array.every()
// In my code, data it would be assigned from an API response;
let data: string[] | string[][] = [];
// Need to use every() and some() for checking
let isChecked = false;
// Check if the type is string[][], got error with every()
if (data.length && data[0] instanceof Array) {
isChecked = data.every(...);
}
// Check if the type is string[], no error with some()
if (data.length && typeof data[0] === 'string') {
isChecked = data.some(...);
}
Typescript would show this error at every():
Each member of the union type
'{ <S extends string>(predicate: (value: string, index: number, array: string[]) => value is S, thisArg?: any): this is S[];
(predicate: (value: string, index: number, array: string[]) => unknown, thisArg?: any): boolean; } | { ...; }'
has signatures, but none of those signatures are compatible with each other
How do I overcome this error. Here an typescript playground example