I have been working on an Angular 14 project where I am implementing a Search Filter Pipe. Below is the code snippet I am using:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'transferFilter'
})
export class TransferFilterPipe implements PipeTransform {
transform(row: any, f1: Date, f2?: Date): any {
f1.toString().length == 0 ? f1 = new Date("1995-12-25T11:30:00.000Z") : f1;
f2 == null ? f2 = new Date() :f2;
if (f1 >= f2 || f1 == null) { return row;}
return row.filter((x: { fecha: string | number | Date; })=>{return new Date(x.fecha) >= new Date(f1) && new Date(x.fecha) <= new Date(f2)});
}
}
However, when testing this code, I encountered the following error message:
No overload matches this call.
Overload 1 of 4, '(value: string | number | Date): Date', gave the following error.
Argument of type 'Date | undefined' is not assignable to parameter of type 'string | number | Date'.
Type 'undefined' is not assignable to type 'string | number | Date'.
Overload 2 of 4, '(value: string | number): Date', gave the following error.
Argument of type 'Date | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.ts(27
The issue seems to be with f2
, highlighted in this part of the code:
new Date(x.fecha) <= new Date(f2)
If anyone has insights on how to resolve this problem, your help would be greatly appreciated!
Thank you.