I've created a simple column filter component so I can filter on a value in a table. All is working as expected but I have some issues when using proper typing for the event emitter.
The relevant code from the ColumnFilterComponent:
export class ColumnFilterEvent {
public value: any;
}
@Component({
selector: 'app-column-filter',
templateUrl: './column-filter.component.html',
styleUrls: ['./column-filter.component.scss'],
})
export class ColumnFilterComponent {
/**
* Event emitted when the filter changes
*/
@Output() public readonly filterChange$: EventEmitter<ColumnFilterEvent> =
new EventEmitter<ColumnFilterEvent>();
/**
* Handle item selections
*/
public onSelectItem(choice: ColumnFilterChoice) {
this.filterChange$.emit({ value: this.selected });
}
}
In the component where the ColumnFilterComponent is used I have the following code:
@Component()
export class MyComponent implements AfterViewInit {
@ViewChild(ColumnFilterComponent) public tbFilter: ColumnFilterComponent;
public ngAfterViewInit(): void {
this.refresh();
// Sorting
const sort$: Observable<Sort> = this.sort.sortChange.pipe(
tap(() => {
this.paginator.pageIndex = 0;
})
);
// Paging
const page$: EventEmitter<PageEvent> = this.paginator.page;
// Filtering
const tbFilter$: EventEmitter<ColumnFilterEvent> = this.tbFilter.filterChange$.pipe(
tap((event: ColumnFilterEvent) => console.log(event))
);
merge(sort$, page$, tbFilter$)
.pipe(tap(() => this.refresh()))
.subscribe();
}
}
Goal is when one of the sort, pagination or filter changes the data is reloaded. However, the above code gives the following error on the const tbFilter$ line:
error TS2740: Type 'Observable<ColumnFilterEvent>' is missing the following properties from type 'EventEmitter<ColumnFilterEvent>': emit, closed, observers, isStopped, and 8 more.
Changing the type to any solves the error but we have a policy against using any.
I've looked at the MatPaginator code and I see no differences. What could be the cause?