The custom RxJS operator shown below, which is essentially a .filter equivalent for demonstration purposes, is currently defined within an Angular 4.0.0-rc.2 component.
declare module 'rxjs/Observable' {
interface Observable<T> {
restrictToCommand<U>(f: (x: T) => U): Observable<U>;
}
}
Observable.prototype.restrictToCommand = function (cmd) {
return new Observable((observer) => {
const obs = {
next: (x) => {
if (x.command === cmd || cmd.indexOf(x.command) !== -1) {
observer.next(x);
}
},
error: (err) => observer.error(err),
complete: () => observer.complete()
};
return this.subscribe(obs);
});
};
The current function signature accepts cmd
as an implicit any
type. I am trying to limit the allowed types by making the following changes:
restrictToCommand<U>(f: (x: T | T[]) => U): Observable<U>;
and
Observable.prototype.restrictToCommand = function (cmd: string | string[]) { ... }
However, it appears that I cannot override the provided type definition due to the compiler error shown below:
ERROR in (...)/mycomponent.component.ts (11,1): Type '(cmd: string | string[]) => Observable<{}>' is not assignable to type '<U>(f: (x: any) => U) => Observable<U>'.
Types of parameters 'cmd' and 'f' are incompatible.
Type '(x: any) => any' is not assignable to type 'string | string[]'.
Type '(x: any) => any' is not assignable to type 'string[]'.
Property 'push' is missing in type '(x: any) => any'.)
What could be causing this issue?