redux-observable 史诗打字稿警告:“store”已声明,但其值从未被读取

问题描述 投票:0回答:2

我有一个

redux observable epic
如下

export const fetchUserEpic:Epic<Action, RootState, any> = 
    (action$: ActionsObservable<Action>, store: MiddlewareAPI<RootState> , { getJSON } ): Observable<Action> => 
        action$.filter((action: Action) => action.type === actionTypes.FETCH_USER)
            .mergeMap(  (action: Action) =>
                getJSON(`/api/user/${action.payload}`)
                .map((response: any) => Actions.fetchUserSuccess(response))
            )

一切正常,测试很棒,但是在编译typescript代码时,我收到以下警告

semantic error TS6133 'store' is declared but its value is never read.

如何解决这个问题

typescript redux redux-observable
2个回答
3
投票

这是来自 TypeScript noUnusedParameters 又名 no-unused-variable 设置的 linting 错误。这意味着您的函数定义了

store
参数,但实际上并未使用它。要修复此问题,您可以在 store 参数前添加下划线
_store
,它将忽略它,或者您可以从 tsconfig 或编译器标志中删除 linting 选项。

(action$: ActionsObservable<Action>, _store: MiddlewareAPI<RootState> , { getJSON } ): Observable<Action>

这之所以有效,是因为 TypeScript 编译器显式添加了此约定作为 linting 规则的例外。它正是针对此类情况而完成的。


0
投票

我知道它有点脏,但我以某种方式找到了这个问题的解决方案,并且解决了警告。

来自代码:

export const fetchUserEpic:Epic<Action, RootState, any> = 
    (action$: ActionsObservable<Action>, store: MiddlewareAPI<RootState> , { getJSON } ): Observable<Action> => 
        action$.filter((action: Action) => action.type === actionTypes.FETCH_USER)
            .mergeMap(  (action: Action) =>
                getJSON(`/api/user/${action.payload}`)
                .map((response: any) => Actions.fetchUserSuccess(response))
            )

我知道

store
变量没有在
function
内的任何地方使用。但由于函数签名需要它,所以我简单地声明一个空对象
{}
而不是
store
变量。

生成的代码将是:

export const fetchUserEpic:Epic<Action, RootState, any> = 
    (action$: ActionsObservable<Action>, {}: MiddlewareAPI<RootState> , { getJSON } ): Observable<Action> => 
        action$.filter((action: Action) => action.type === actionTypes.FETCH_USER)
            .mergeMap(  (action: Action) =>
                getJSON(`/api/user/${action.payload}`)
                .map((response: any) => Actions.fetchUserSuccess(response))
            )

并且警告得到解决。如果我需要在史诗中使用store,我可以简单地在函数参数中定义

store
变量。

© www.soinside.com 2019 - 2024. All rights reserved.