我想根据操作类型得出props a反应组件的类型。由于操作类型总是返回操作对象和派遣的道具将没有返回类型,因此我们如何像预期类型一样获取正确的派遣支架类型?
export type Action =
({ type: 'PING', arg: number })
const ping = (arg: number): Action => ({
type: 'PING', arg
})
interface Props {
ping: typeof ping;
}
const PingTestComponent: React.SFC<Props> = ({ping}) => {
return (
<Button onPress={() => ping(123)} title="ping"/>
);
};
export const PingTest = connect(
null,
({ ping: ping })
)(PingTestComponent);
我期望派遣的道具解决的类型为
预期
interface Props {
ping: (arg: number) => void
}
实际上是连接到动作创建者的返回类型,而不是void
。
无论如何,这就是您可以更改函数的返回类型的方式:
declare function foo(bar: string, baz: boolean): { type: 'A' };
type WithReturnVoid<T extends (...arg) => any> = (...args: Parameters<T>) => void;
type VoidFoo = WithReturnVoid<typeof foo>; // (bar: string, baz: boolean) => void
游乐场
Parameters
是内置实用程序,允许获取函数参数的类型。
因此,在您的示例中,它将是:
interface Props {
ping: WithReturnVoid<typeof ping>;
}