RxJs6:OperatorFunction 与 MonoTypeOperatorFunction
RxJs6: OperatorFunction vs MonoTypeOperatorFunction
我有以下代码:
export class EventsChainComponent {
eventSubscriber:Subscription;
constructor (protected eventService: EventService) {}
public registerComponentsEvent(event:any) {
// getOnEvent signature
// (method) EventService.getOnEvent(): Observable<FormEvent>
this.eventSubscriber = this.eventService.getOnEvent()
.pipe(filter((formEvent: FormEvent) => {return formEvent.key == event.event}))
.subscribe((formEvent: FormEvent) => {
..........
});
}
我编译的时候,编译器returns出现如下错误:
Argument of type 'MonoTypeOperatorFunction' is not assignable to parameter of type 'OperatorFunction'.
所以,我搜索了一下,找到了 RxJs6
运算符过滤器 API:
export declare function filter<T, S extends T>(predicate: (value: T, index: number) => value is S, thisArg?: any): OperatorFunction<T, S>;
export declare function filter<T>(predicate: (value: T, index: number) => boolean, thisArg?: any): MonoTypeOperatorFunction<T>;
如您所见,过滤器作为 2 个重载方法,一个返回 OperatorFunction
另一个 MonoTypeOperatorFunction
.
谁能告诉我这两种类型的区别?谁知道我该如何解决这个错误?
注意:FormEvent
class 是我创建的,EventService
和 EventsChainComponent
具有相同的导入,引用相同的 class .
在下面的 之后,我发现问题是我从相同的文件名导入了 class,但是我在不同的文件夹中有相同的文件,在 src 文件夹下.
The only way that a MonoTypeOperatorFunction<T>
is not assignable to
an OperatorFunction<T,T>
is if the type parameters - the Ts
- are
different. Saying that the function returns Observable<FormEvent>
in a
comment is not particularly useful. Where does FormEvent event come
from? Is it the same as the FormEvent you've used in that file? Is it
a class? Who knows? Not me. My guess is that you have two FormEvent
classes from different libraries. Or different installs of the same
library.
我所做的是删除其中一个文件,错误消失了。
对于遇到此问题的其他人:
我遇到了同样的问题。在我的例子中,问题出在事件对象上。
rxjs 和 angular/router 都有一个事件对象,因此存在名称冲突。
我为解决这个问题所做的是用不同的名称声明 @angular/router 事件,然后相应地使用。
{Event as RouterEvent} from '@angular/router';
所以在那之后:当我想使用 rxjs.Event 时我使用 Event
,当我想使用 @angular/router.Event 时我使用 RouterEvent
。
错误消失了:)
希望对您有所帮助
提到的解决方案对我不起作用...我找到了这个
filter((event): event is ActivationEnd => event instanceof ActivationEnd)
致谢
https://gitmemory.com/issue/ReactiveX/rxjs/4947/518146427
我有以下代码:
export class EventsChainComponent {
eventSubscriber:Subscription;
constructor (protected eventService: EventService) {}
public registerComponentsEvent(event:any) {
// getOnEvent signature
// (method) EventService.getOnEvent(): Observable<FormEvent>
this.eventSubscriber = this.eventService.getOnEvent()
.pipe(filter((formEvent: FormEvent) => {return formEvent.key == event.event}))
.subscribe((formEvent: FormEvent) => {
..........
});
}
我编译的时候,编译器returns出现如下错误:
Argument of type 'MonoTypeOperatorFunction' is not assignable to parameter of type 'OperatorFunction'.
所以,我搜索了一下,找到了 RxJs6
运算符过滤器 API:
export declare function filter<T, S extends T>(predicate: (value: T, index: number) => value is S, thisArg?: any): OperatorFunction<T, S>;
export declare function filter<T>(predicate: (value: T, index: number) => boolean, thisArg?: any): MonoTypeOperatorFunction<T>;
如您所见,过滤器作为 2 个重载方法,一个返回 OperatorFunction
另一个 MonoTypeOperatorFunction
.
谁能告诉我这两种类型的区别?谁知道我该如何解决这个错误?
注意:FormEvent
class 是我创建的,EventService
和 EventsChainComponent
具有相同的导入,引用相同的 class .
在下面的
The only way that a
MonoTypeOperatorFunction<T>
is not assignable to anOperatorFunction<T,T>
is if the type parameters - theTs
- are different. Saying that the function returnsObservable<FormEvent>
in a comment is not particularly useful. Where does FormEvent event come from? Is it the same as the FormEvent you've used in that file? Is it a class? Who knows? Not me. My guess is that you have two FormEvent classes from different libraries. Or different installs of the same library.
我所做的是删除其中一个文件,错误消失了。
对于遇到此问题的其他人:
我遇到了同样的问题。在我的例子中,问题出在事件对象上。
rxjs 和 angular/router 都有一个事件对象,因此存在名称冲突。 我为解决这个问题所做的是用不同的名称声明 @angular/router 事件,然后相应地使用。
{Event as RouterEvent} from '@angular/router';
所以在那之后:当我想使用 rxjs.Event 时我使用 Event
,当我想使用 @angular/router.Event 时我使用 RouterEvent
。
错误消失了:)
希望对您有所帮助
提到的解决方案对我不起作用...我找到了这个
filter((event): event is ActivationEnd => event instanceof ActivationEnd)
致谢 https://gitmemory.com/issue/ReactiveX/rxjs/4947/518146427