【问题标题】:Type for a redux action creatorredux 动作创建者的类型
【发布时间】:2019-10-01 08:19:30
【问题描述】:

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

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

其中 ThunkResult 是 ThunkResult&lt;R&gt; = ThunkAction&lt;R, IStoreState, null, ActionTypes&gt;

我不清楚为什么左侧函数的返回类型是ActionCreator&lt;ThunkResult&lt;void&gt;&gt;,而右侧是ThunkResult&lt;void&gt;

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

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

【问题讨论】:

    标签: typescript redux redux-thunk


    【解决方案1】:

    我可以让你更容易理解 首先,只要认为 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
    

    希望对你有帮助

    【讨论】:

    • 那么我做对了吗,基本上 ActionCreator 和 ThunkResult 都有一个类型“函数”而不是这些函数的返回值类型?
    • 没错。 :----)
    猜你喜欢
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多