Visual Studio 无法识别 NgRx ofType() return 类型(显示从不类型,仍然可以在浏览器中编译并运行良好)

Visual Studio does not recognize NgRx ofType() return type (showing never type, still compiles and runs well in browser)

我有以下使用 NgRx 的代码 Effect:

import { Injectable } from "@angular/core";
import { Actions, ofType, Effect } from "@ngrx/effects";
import { SearchContactService } from "../../search-contact.service";
import { Action } from "@ngrx/store";
import { Observable, of } from "rxjs";
import { SearchActionTypes, SearchTagResultAction, SearchKeywordResultAction } from "../actions/search.action";
import { mergeMap, map, debounceTime, distinctUntilChanged, filter } from "rxjs/operators";

@Injectable()
export class SearchEffects {

    @Effect()
    enterSearch$: Observable<Action> = this.actions$.pipe(
        ofType(SearchActionTypes.SearchEnterKeyword),
        filter(action => { return action && action.payload.keyword && action.payload.keyword.length }),
        debounceTime(300),
        distinctUntilChanged(),
        mergeMap(action => {
            let keyword = action.payload.keyword as string;

            if (keyword.startsWith("#")) {
                return this.searchContactService.searchTag(keyword.substr(1))
                    .pipe(
                        map(data => new SearchTagResultAction({
                            tags: data,
                        }))
                    );
            } else {
                return this.searchContactService.searchContact(keyword)
                    .pipe(
                        map(data => new SearchKeywordResultAction({
                            contacts: data,
                        }))
                    );
            }
        }),
    );

    constructor(
        private searchContactService: SearchContactService,
        private actions$: Actions) {

    }

}

代码在浏览器中编译和运行良好。但是,VS 在每个 action 参数处显示错误非常烦人。

有人遇到过这个问题吗?我该如何解决?

编辑:我发现这是因为 ofType 方法,而不是 pipe 本身。仍然不明白为什么。

编辑 2:为 ofType 添加类型后,另一个赋值错误。在浏览器中,它仍然编译并运行。我什至尝试了 SearchActions(我的联合操作类型),同样的问题仍然存在。

编辑:找到解决方案:也在 mergeMap 上使用显式类型:

mergeMap<SearchEnterKeywordAction, SearchActions>(action => {

您必须输入 ofType 运算符:

ofType<SearchEnterKeyword>(SearchActionTypes.SearchEnterKeyword),

从 NgRx 7 开始,您还可以输入由 @ngrx/effects

注入的 Actions
constructor(private actions$: Actions<MyActionsUnion>) {}