Visual Studio无法识别NgRx ofType()返回类型(显示永不键入,仍可在浏览器中编译并正常运行)

时间:2019-01-02 17:42:40

标签: angular visual-studio typescript ngrx visual-studio-2019

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

enter image description here

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(我的联合操作类型),仍然遇到相同的问题。

enter image description here

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

mergeMap<SearchEnterKeywordAction, SearchActions>(action => {

1 个答案:

答案 0 :(得分:1)

您必须输入ofType运算符:

ofType<SearchEnterKeyword>(SearchActionTypes.SearchEnterKeyword),

自NgRx 7起,您还可以输入Actions注入的@ngrx/effects

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