代码之家  ›  专栏  ›  技术社区  ›  Ricardo Rocha

RxJs6:运算符函数与单类型运算符函数

  •  1
  • Ricardo Rocha  · 技术社区  · 6 年前

    我有以下代码:

    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) => {
                      ..........
                });
        }
    

    编译时,编译器返回以下错误:

    所以,我搜索了一下,找到了 RxJs6

    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>;
    

    如您所见,filter As 2重载方法,其中一个返回 OperatorFunction 还有一个 MonoTypeOperatorFunction .

    有人能告诉我这两种类型的区别吗?有人知道我该怎么解决这个错误吗?

    注:以下为 FormEvent 类是由我创建的,两者都是 EventService EventsChainComponent 具有与引用同一类相同的导入。

    2 回复  |  直到 6 年前
        1
  •  4
  •   Andreas Hadjithoma    6 年前

    对于有此问题的其他ppl:

    我也有同样的问题。在我的例子中,问题在于事件对象。

    rxjs和angular/router都有一个事件对象,因此发生了名称冲突。 我解决这个问题的方法是用不同的名称声明@angular/router事件,然后相应地使用它。

    {Event as RouterEvent} from '@angular/router';
    

    所以在那之后:我用 Event 当我想使用rxjs.Event和 RouterEvent 当我想使用@angular/router.Event时。

    希望有帮助

        2
  •  2
  •   T04435    6 年前

    之后 comment 下面,我发现问题是我从相同的文件名导入了类,但是我在src文件夹下的不同文件夹中有相同的文件。

    唯一的办法 MonoTypeOperatorFunction<T> 不可分配给 OperatorFunction<T,T> 如果类型参数 Ts 与众不同。表示函数返回 Observable<FormEvent> 在一个 一节课?谁知道呢?不是我。我猜你有两个问题 来自不同库的类。或相同的不同安装 图书馆。

        3
  •  0
  •   yLoeffler    4 年前

    上面提到的解决方案对我不起作用。。。我找到这个了

    filter((event): event is ActivationEnd => event instanceof ActivationEnd)
    

    贷记 https://gitmemory.com/issue/ReactiveX/rxjs/4947/518146427