Redux如何使用Typescript"compose"

tom*_*cak 10 typescript redux

我在Typescript中使用compose函数有点问题,我总是收到错误..d.ts中的类型定义也很混乱.例如:

type Props = { t: number }; 
const foo = (props: {}) => <div {...props} />;
const moo = (props: Props) => <div {...props} />;
const bar = (props: Props) => <div {...props} />;
const Dar = compose(foo, moo)(bar);

const Test = () => <Dar />;
Run Code Online (Sandbox Code Playgroud)

有几个问题.它抱怨"bar"没有"foo"参数(它实际上有).

另外,我无法使用,因为Dar被评估为JSX.Element而不是无状态函数.任何IDEA都有可能的例子,如何在Typescript中使用compose?

谢谢.

Rei*_*ans 0

compose是一个组合两个函数的函数,假设第一个函数的输出类型与第二个函数的输入类型匹配。

const compose = (f,g) => x => f(g(x))

因此,您在撰写行中所说的是,您想要应用(这是一个来自tobar的函数)作为参数,该参数也是来自to 的函数,然后获取其结果并将其应用到。 PropsJSX.ElementmooPropsJSX.Elementfoo

问题是不接受 from to 的moo函数,它接受PropsJSX.ElementProps

如果你想返回一个“无状态函数”,你需要使用这个模板来告诉 Typescript 这就是你想要的

const bar: React.FunctionComponent<Props> = (x: Props) => <div {...props} />;
Run Code Online (Sandbox Code Playgroud)