【发布时间】:2019-04-23 20:51:19
【问题描述】:
我有一个习惯,在 redux 中传递信息时,我似乎重复了很多类型。当它们都在 ActionCreators 中定义时,有什么方法可以自动生成接口 Props?请看下面的代码:
import { bindActionCreators, Dispatch } from "redux";
const ActionCreators = {
foo: (a: string): string => ("foo" + a),
bar: (a: number): string => ("bar" + a),
baz: (a: boolean): number => (a ? 256 : 123)
};
interface Props {
foo: (a: string) => string;
bar: (a: number) => string;
baz: (a: boolean) => number;
}
const mapDispatchToProps = (dispatch: Dispatch): Props => {
return bindActionCreators(ActionCreators, dispatch);
};
不需要了解 bindActionCreators,这里真正的问题是获取 ActionCreators 上的所有签名,然后将其提取到诸如 Props 之类的接口中。
【问题讨论】:
标签: typescript interface react-redux typescript-typings