我有一个反应-最终的形式,我希望它与它自己的类型打字。但是,“子(RenderRest)”给出了以下错误:
无法调用类型缺少调用签名的表达式。输入'string number has {} ReactElement ReactElement Component)> AC.26 null) \x(新的(props: any) =>组件)>{props:FormRenderProps => ReactNode)‘没有兼容的call signatures.ts(2349)
我的代码是:
import React from "react";
import styled from "styled-components";
import { Form as StateForm, FormProps } from "react-final-form";
import Box from "src/App/Components/Layout/Box";
const StyledForm = styled(Box)``;
const Form = ({ children, ...rest }: FormProps) => {
return (
<StateForm
{...rest}
render={({ handleSubmit, ...renderRest }) => (
<StyledForm as="form" onSubmit={handleSubmit}>
{children && children(renderRest)}
</StyledForm>
)}
/>
);
};
export default React.memo(Form);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
这个错误“不能调用类型缺乏调用签名的表达式”,在我能够解决的各种情况下都发生了。但不幸的是在这个案子上没有。
有什么提示吗?谢谢
发布于 2019-05-10 14:14:18
在react-final-form
的类型定义中,它将children
在RenderableProps
( FormProps
扩展)中定义为:
export interface RenderableProps<T> {
children?: ((props: T) => React.ReactNode) | React.ReactNode;
component?: React.ComponentType<T> | string;
render?: (props: T) => React.ReactNode;
}
因此,children
要么是一个接受props
并创建React.ReactNode
的函数,要么是一个直接的React.ReactNode
实例。在后一种情况下,您不能像使用函数一样使用它。因为它可能是两种方式之一,TS只允许您做两者之间的共同之处。错误Cannot invoke an expression whose type lacks a call signature.
基本上是说,“您试图使用children
,就像它是一个函数一样,这是不允许的,因为我不确定它是否是一个函数。”
我不太了解react-final-form
库,不能说出解决这个问题的最佳方法。你可以投..。
(children as ((props: T) => React.ReactNode))(renderRest);
...or你可以添加一个类型保护..。
if (typeof children === 'function') {
children(renderRest);
}
https://stackoverflow.com/questions/56076608
复制相似问题