typescript - 从对象中提取打字稿签名到接口中
问题描述
我有一个习惯,在 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 之类的接口。
解决方案
您可以只使用typeof
类型运算符来获取任何常量的类型。然后,您可以使用类型别名为其命名
const ActionCreators = {
foo: (a: string): string => ("foo" + a),
bar: (a: number): string => ("bar" + a),
baz: (a: boolean): number => (a ? 256 : 123)
};
type Props = typeof ActionCreators;
/*
Same as
type Props = {
foo: (a: string) => string;
bar: (a: number) => string;
baz: (a: boolean) => number;
}
*/
虽然在这种情况下接口和类型别名之间存在细微差别,但它们应该是等效的。
编辑
评论中的后续问题:如何将所有成员函数的返回类型更改为 void ?
为此,您需要使用映射类型将原始类型映射到新类型,并使用条件类型来提取原始函数的参数类型:
type ArgumentTypes<T> = T extends (...a: infer A) => any ? A: [] //Conditional type extracts the argument types
type Props = {
// Mapped type, maps the keys of the original type to a new type
// with the same keys, and with each key being a function with the same argument as the original
// but returning void.
[P in keyof typeof ActionCreators]: (...a: ArgumentTypes<typeof ActionCreators[P]>) => void
}
推荐阅读
- r - 作为数据框的 Princomp 摘要
- angular - Angular 6 RequestOptions 搜索已折旧
- django - 使用 Django 创建或更新实例时发送电子邮件
- c# - Xamarin.Android 中的数据库
- ios - Apple 应用内购买 - 从销售中删除商品后,活跃订阅会发生什么
- r - 从 mcmc 对象中提取系数
- javascript - React 如何在表单中跳转到目标输入区域?
- matlab - Matlab trimesh投影矩阵
- windbg - windbg 诊断 64 位转储中的泄漏 - !堆未显示内存增长
- excel - VBA GetAsyncKeystate