'TypeScript: Why is this return value unknown when the callback function is typed?

I'm trying to figure out how to type this fmap function I have. Here's my latest attempt:

export const __skip__ = Symbol('skip');

type ReduceFn<T> = Array<T>['reduce']

/**
 * Filter-map. Like map, but you may omit entries by returning `__skip__`.
 */
export function fmap<TVal,TReturn>(this:TVal[], callback: (this: TVal[], ...args:[currentValue: TVal, index: number])=>TReturn|typeof __skip__): TReturn[] {
    return Array.prototype.reduce.call(this, (accum: TReturn[], ...args) => {
        let x = callback.call(this, ...args);
        if (x !== __skip__) {
            accum.push(x);
        }
        return accum;
    }, []);
}

playground

It's saying x is unknown

enter image description here

Even though it knows what callback returns:

enter image description here

How come? How do I fix the errors?

N.B. I typed args as [currentValue: TVal, index: number] to try to simplify a bit, but I think it should be Parameters<ReduceFn<TVal>>


Also, I know the signature is a bit funny. The usage of this is intentional for legacy reasons.

Here's an example usage:

const keys = fmap.call(feeIds, id => feeIdToKey[id] || __skip__)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source