dan dan - 3 months ago 28
TypeScript Question

Typescript error on callback function: Type 'Function' provides no match for the signature

I'm an error that Type 'Function' provides no match for the signature for the filter below. Which is true because filter expects a specific type. How would define my callback to match what filter expects?

private _getItemFilteredBy(itemName: string, Fn: Function): Observable<any[]> {
return this.getItemByName(itemName)
.map((items: any[]) => {
return items.filter( Fn );
});
}

Answer

Filter needs a predicate. Change the type to (x:any) => boolean

private _getItemFilteredBy(itemName: string, Fn: (x:any) => boolean): Observable<any[]> {
    return this.getItemByName(itemName)
        .map((items: any[]) => {
            return items.filter( Fn );
        });
}

If you have more specific type information than 'any', I'd recommend updating that too but what I've posted above should work.