Can't call Array.every() with typescript union type

Viewed 411

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

2 Answers
let a: string[] | string[][] = [];


const isArrOfArr = (arg: any): arg is string[][] => Array.isArray(arg) && Array.isArray(arg[0])

const isArrOfArr_super_safe = (arg: any): arg is string[][] => Array.isArray(arg) && arg.every(Array.isArray)


if (isArrOfArr(a)) {
    let b = a.every(elem => elem) // ok
}

TS does not treat data[0] instanceof Array as a typeguard for for first element because arrays are mutable by default.

You can use your own typeguard for such purpose

You can do something like this. Code is a little lengthy but it will serve the purpose of type safety.

let a: Array<string> | Array<Array<string>> = [];


if (a.length && a[0] instanceof Array) {
    let b = (a as Array<Array<string>>).every((item: Array<string>) => {
        if (typeof item === "string" && item) return true
        else {
            if (Array.isArray(item)) {
                return item.every(subItem => subItem)
            }
            return false
        }
    })
    console.log(b)
}

Here is the playground link

Related