首页 > 解决方案 > 从对象中提取打字稿签名到接口中

问题描述

我有一个习惯,在 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 之类的接口。

标签: typescriptinterfacereact-reduxtypescript-typings

解决方案


您可以只使用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
}

推荐阅读