After researching about type guards in TypeScript at this source and this source, I still encountered compiler errors.
Error:(21, 14) TS2349: Cannot invoke an expression whose type lacks a call signature. Type '{ (callbackfn: (value: Foo, index: number, array: Foo...' has no compatible call signatures.
I have the following classes:
Foo.ts
export class Foo {
expired: boolean;
}
Bar.ts
export class Bar {
foo: Foo;
}
MyPipe.ts
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({
name: 'myPipe'
})
export class MyPipe implements PipeTransform {
transform(items: Foo[] | Bar[], isExpired: Boolean): Foo[] | Bar[] {
if (!items) {
return items;
}
if (items[0] instanceof Foo) {
return items.filter((foo: Foo) => {
return foo.expired == isExpired;
});
} else {
return items.filter((bar: Bar) => {
return bar.foo.expired == isExpired;
});
}
}
}
My query is how can I effectively utilize union binding for my parameter "items" while also integrating type guard usage in my Angular pipe using TypeScript?