gpt4 book ai didi

javascript - 如何使用流类型绑定(bind)异步操作创建者?

转载 作者:数据小太阳 更新时间:2023-10-29 04:21:14 25 4
gpt4 key购买 nike

我刚开始学习 flowtype,我需要一些帮助来理解我头脑中不清楚的两件事。

  1. 使用 https://github.com/reactjs/redux/blob/master/examples/todos-flow例如,我想知道在没有 https://github.com/flowtype/flow-typed 类型定义的情况下如何控制类型,在这种情况下:https://github.com/flowtype/flow-typed/blob/master/definitions/npm/redux_v3.x.x/flow_v0.33.x-/redux_v3.x.x.js ?

  2. 如果我使用 redux 定义,当我尝试绑定(bind)异步操作创建器(我使用的是 redux-thunk)时,bindActionCreators 的验证失败。

如何在使用 redux-thunk 的时候继续使用 flow 和 bind async actions creators?

代码示例(https://gist.github.com/momsse/323c228e8c5e264067039b8446cd890f):

import { bindActionCreators } from 'redux';
import type { Dispatch } from 'redux';

type Action = { type: 'SET_PROFILE', profile: Object };

/**
* Based on https://github.com/gaearon/redux-thunk/blob/master/index.d.ts
*/
type ThunkAction = (dispatch: Dispatch<Action>,
getState: () => any,
extraArgument: any) => any;

type Profile = {
name: string,
team: string
}

// Async actions creator
function setProfile(profile: Profile): ThunkAction {
return dispatch => setTimeout(() => dispatch({ type: 'SET_PROFILE', profile }), 2000);
}

const profileActionCreators = { setProfile };

type Props = {
actions: {
setProfile: (profile: Profile) => ThunkAction,
}
}

function mapDispatchToProps(dispatch: Dispatch<Action>): Props {
return {
actions: bindActionCreators(profileActionCreators, dispatch)
};
}

错误:

 40:     actions: bindActionCreators(profileActionCreators, dispatch)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function call. Function cannot be called on any member of intersection type
40: actions: bindActionCreators(profileActionCreators, dispatch)
^^^^^^^^^^^^^^^^^^ intersection
Member 1:
49: declare function bindActionCreators<A, C: ActionCreator<A, any>>(actionCreator: C, dispatch: Dispatch<A>): C;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ polymorphic type: function type. See lib: flow-typed/npm/redux_v3.x.x.js:49
Error:
49: declare function bindActionCreators<A, C: ActionCreator<A, any>>(actionCreator: C, dispatch: Dispatch<A>): C;
^^^^^^^^^^^^^^^^^^^^^ function type. Callable signature not found in. See lib: flow-typed/npm/redux_v3.x.x.js:49
40: actions: bindActionCreators(profileActionCreators, dispatch)
^^^^^^^^^^^^^^^^^^^^^ object literal
Member 2:
50: declare function bindActionCreators<A, K, C: ActionCreators<K, A>>(actionCreators: C, dispatch: Dispatch<A>): C;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ polymorphic type: function type. See lib: flow-typed/npm/redux_v3.x.x.js:50
Error:
13: declare type Dispatch<A: { type: $Subtype<string> }> = (action: A) => A;
^^^^^^^^^^^^^^^^^^^^^^^^^^ property `type` of object type. Property not found in. See lib: flow-typed/npm/redux_v3.x.x.js:13
21: function setProfile(profile: Profile): ThunkAction {
^^^^^^^^^^^ function type

最佳答案

这些是 ActionCreator 和 bindActionCreators 的完整声明:

  declare type ActionCreator<A, B> = (...args: Array<B>) => A;
declare type ActionCreators<K, A> = { [key: K]: ActionCreator<A, any> };

declare function bindActionCreators<A, C: ActionCreator<A, any>>(actionCreator: C, dispatch: Dispatch<A>): C;
declare function bindActionCreators<A, K, C: ActionCreators<K, A>>(actionCreators: C, dispatch: Dispatch<A>): C;

在您的代码中,bindActionCreators 会将 profileActionCreators 的每个属性包装在 dispatch 中。您似乎期望将 dispatch 传递给 setProfile 函数,稍后可以将 dispatch 用作回调。

但在我看来,bindActionCreators 似乎不支持将“绑定(bind)”分派(dispatch)作为回调。相反,调度是这样“绑定(bind)”的:

function bindActionCreator(actionCreator, dispatch) {
return (...args) => dispatch(actionCreator(...args))
}

所以在你的代码中,它实际上看起来像这样:

function mapDispatchToProps(dispatch: Dispatch<Action>): Props {
return {
actions: {
setProfile: (profile) => dispatch(dispatch => setTimeout(() => dispatch({ type: 'SET_PROFILE', profile }), 2000)),
};
}

因此,Flowtype 正确地捕获了类型错误,表示 bindActionCreators 期望对象的每个属性都是一个 actionCreator:() => Action

您可能无法将 bindActionCreators 用于您的用例,或者您需要重新考虑如何处理 thunk。 Here is an approach that should work .

const profileActionCreators = { setProfile };

type Props = {
actions: {
setProfile: (profile: Profile) => setTimeout,
}
}

function mapDispatchToProps(dispatch: Dispatch<Action>): Props {
const boundActions = bindActionCreators(
profileActionCreators,
dispatch
);
return ({
actions: {
setProfile: (profile: Profile) => setTimeout(() => boundActions.setProfile(profile), 2000),
},
});
}

Thunk 方法

如果您想保留您的 ThunkAction 方法,您将无法使用 bindActionCreators。 This also works.

关于javascript - 如何使用流类型绑定(bind)异步操作创建者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40620636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com