为 redux action creator 输入

Type for a redux action creator

我目前对我正在开发的 react-redux 应用程序中的 action creator 类型感到困惑。我有这个 action creator,等式两边都有类型,TS 编译器说一切都很完美:

const setItem: ActionCreator<ThunkResult<void>> = (...): ThunkResult<void> => (...): void => {
  dispatch({
    ...
  });
};

ThunkResult 是 ThunkResult<R> = ThunkAction<R, IStoreState, null, ActionTypes>

我不清楚为什么在左侧函数的返回类型是 ActionCreator<ThunkResult<void>> 但是在右侧它是 ThunkResult<void>.

注意:我跳过了参数,因为它们在这个问题中并不重要。

不知道是对TS functions的误解还是TS的问题

我可以让你更容易理解 首先,假设 ActionCreator 是一个 Type 并且它将是

type ActionCreator<T> = (params) : T => T
// then you can defined the variable
const setItem: ActionCreator<string> = (params) : string => ""

喜欢ThunkResult

type ThunkResult<T> = (params): T => T
// then you can defined the variable
const thunk: ThunkResult<number> = (params): number => 0

所以,如果你把它们混合起来就会像这样

ActionCreator<ThunkResult<void>> = (params) : ThunkResult<void> => ThunkResult<void>
// and 
ThunkResult<void> = (params):void => void
// so
ActionCreator<ThunkResult<int>> = (params): ThunkResult<void> => (params2): void => void // just replace ThunkResult<void> above with (params):void => void

希望对您有所帮助