Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript type guard function infers wrong type when using array reduce

Returning the result of the reduce function gives a type error, but saving the result in a variable (isValid) and then returning the variable doesn't give any error.

Since both methods are semantically equivalent I wonder if this is a bug in TS type inference system.

TypeScript Playground

interface Params {
    a: string;
    b: number;
}

const paramKeys = ["a", "b"] as const;

// Doesn't work -> Type 'string' is not assignable to type 'boolean'.
const isHydrationParamsValid = (params: any): params is Params => {
    return paramKeys.reduce((_, curr) => {
        if (!params[curr]) {
            console.warn(`Hydration param ${curr} is missing`);
            return false;
        }
        return true;
    }, true);
};

// Works!
const isHydrationParamsValid2 = (params: any): params is Params => {
    const isValid = paramKeys.reduce((_, curr) => {
        if (!params[curr]) {
            console.warn(`Hydration param ${curr} is missing`);
            return false;
        }
        return true;
    }, true);
    return isValid;
};

like image 374
rareyesdev Avatar asked Jan 19 '26 22:01

rareyesdev


1 Answers

There are already a lot of issues regarding the type inference for reduce in TS Github, the one collecting them all is Array method definition revamp: Use case collection #36554

I believe your method falls into the same error category as the one in comment:

// the return type here is correctly inferred as `boolean`
function works() {
  return new Array('foo', 'bar').reduce(
    (previousValue, currentValue) => !!(previousValue && currentValue),
    true
  )
}

function broken(): boolean {
  return new Array('foo', 'bar').reduce(
    (previousValue, currentValue) => !!(previousValue && currentValue),
    true
  )
}
like image 63
Lesiak Avatar answered Jan 22 '26 10:01

Lesiak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!