Search code examples
typescripttypestype-conversionunion-types

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


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


Solution

  • 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