我想输入一个不同于分派类型的操作创建者。
我尝试用这两种类型来提示ThunkResult,但这并不理想。
// types.ts
interface AppListAction extends FetchAction {
type: typeof APP_LIST,
[FETCH]: {},
}
interface AppListSuccessAction extends FetchResponseAction {
type: typeof APP_LIST_SUCCESS,
response: Array<ListModel>
}
export type AppResponseActions =
AppListSuccessAction
export type AppActions =
AppListAction
| AppResponseActions
// actions.ts
export const loadListCreator = (): AppActions => ({
type: C_LIST,
[FETCH]: {},
})
export const loadList = (): ThunkResult<AppResponseActions> => (dispatch: ThunkDispatcher) => dispatch(loadListCreator())
我预计不会出现错误,但我收到的却是:
TypeScript错误:类型'AppActions‘不可分配给类型'AppResponseActions’。类型'AppListAction‘不可赋值给类型'AppResponseActions’。类型“”AppListAction“”中缺少属性“”response“”,但类型“”AppListSuccessAction“”中需要该属性。“”TS2322
发布于 2019-05-27 03:47:15
为了使您的代码可编译,我做了以下假设
import { ThunkAction, ThunkDispatch } from 'redux-thunk';
const FETCH = 'fetch';
interface FetchAction {
[FETCH]: object;
}
interface FetchResponseAction {
prop2: string;
}
const APP_LIST = 'APP_LIST';
const APP_LIST_SUCCESS = 'APP_LIST_SUCCESS';
interface ListModel {
prop3: string;
}
在你的代码之后
type ThunkResult<R> = ThunkAction<R, State, undefined, AppActions>;
type ThunkDispatcher = ThunkDispatch<State, undefined, AppActions>;
根据上面的假设,您的代码可以通过以下修改进行编译
export const loadList = (): ThunkResult<AppActions> => (dispatch: ThunkDispatcher) => dispatch(loadListCreator());
ThunkResult<AppActions>
的类型应为R = AppActions
。R
是ThunkResult
类型的函数的返回值。在Redux-Thunk源代码中,ThunkAction
的定义如下
export type ThunkAction<
TReturnType,
TState,
TExtraThunkArg,
TBasicAction extends Action
> = (
dispatch: ThunkDispatch<TState, TExtraThunkArg, TBasicAction>,
getState: () => TState,
extraArgument: TExtraThunkArg
) => TReturnType;
因此ThunkAction
( ThunkResult
所基于的)是一个返回值为TReturnType
类型的函数。
然后尝试使ThunkResult<>
类型的loadlist
等于(dispatch: ThunkDispatcher) => dispatch(loadListCreator())
。本质上也是函数,但返回的是dispatch
调用的结果。dispatch
的类型为ThunkDispatcher
,from source的类型为
export interface ThunkDispatch<
TState,
TExtraThunkArg,
TBasicAction extends Action
> {
<TReturnType>(
thunkAction: ThunkAction<TReturnType, TState, TExtraThunkArg, TBasicAction>
): TReturnType;
<A extends TBasicAction>(action: A): A;
}
所以它是一个重载函数,接受一个类型为A
的参数,并返回类型为A
的结果。由于dispatch
是用返回AppActions
类型结果的loadListCreator()
调用的结果调用的,因此A
被推断为AppActions
。
由于您希望将返回AppActions
的函数分配给返回AppResponseActions
的函数,因此未正确编译原始代码。AppActions
是联合类型,可以是AppResponseActions
或AppListAction
。如果它是AppListAction
,则不能将其分配给AppResponseActions
。
我想我已经描述得很清楚了:-)
从实践的角度来看,你的代码有点过于复杂了。
考虑这样简化它。
从‘redux’导入{ AppListAction };接口APP_LIST扩展操作{ type: type to APP_LIST,FETCH:{},}
ThunkResult
为以下类型类型ThunkResult = ThunkAction;
最有可能的是,您不需要TReturnType
。(void
).
接口SyncAction1扩展操作{类型:'SYNC_ Action ',参数:字符串}常量SomeSyncAction =(参数:字符串) => {类型:'SYNC_ action ',参数}用于异步操作的
导出常量结果= ():ThunkResult结果( =>:ThunkDispatcher) => { dispatch(/*开始加载*/);const result = await //加载接口调用分派(loadListCreator(Result));}
导出函数状态( loadReducer = initalState,动作: AppActions) // reducer代码
https://stackoverflow.com/questions/56218221
复制