Issue
I have the following model :
export interface IModel<T> {
exp: number,
value: T
}
I want to create a custom RxJS operator such as :
private customOperator<U extends A | B, T extends IModel<U>>(custom: Observable<T>): Observable<U> {
return custom.pipe(map((x: T) => x.value ));
}
But I Have a type error when using it :
mySub: = new Subject<IModel<A>>;
myObs$: Observable<A> = this.mySub.asObservable().pipe(this.customOperator); // <== ERROR
The error : Can't assign type 'Observable<A | B>' to type 'Observable< A>'.
Any idea on how I could change my custom operator to avoid the issue ?
Solution
Wrap the operator in a factory function.
private customOperator<T extends IModel<U>, U extends A | B> () {
return (custom: Observable<T>): Observable<U> =>
custom.pipe(map((x: T) => x.value ));
}
And then in the pipe use the operator as a function call instead of the function ref.
myObs$: Observable<A> = this.mySub.asObservable().pipe(this.customOperator());
cheers
Answered By - akotech
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.