当我尝试创建antd自定义表单时,Typescript构建失败

时间:2019-06-21 07:10:06

标签: javascript reactjs typescript antd

我有一个名为 admin.find({id:"5d089264d8aae228d7f10c1d"}).populate("users").then(data=>{ //update the fields which you would find in the data object data.save() //your data would be changed. }) 的React组件,我正在尝试创建一个form using antd。但是,当我构建打字稿应用程序时,出现错误。

BasicDelete

错误提示:

// This is how I am trying to create the form component

const BasicDeleteForm = Form.create({
    mapPropsToFields(props: {
        workFlowData: {
        reason: string;
        };
    }) {
        return {
        reason: Form.createFormField({
            value: props.workFlowData.reason
        })
        };
    }
})(BasicDelete);

为什么会出现此错误?如何正确防止此错误?

1 个答案:

答案 0 :(得分:0)

您必须在TypeScript中指定类型:

import { Form } from 'antd';
import { FormComponentProps } from 'antd/lib/form';

interface UserFormProps extends FormComponentProps {
  age: number;
  name: string;
}

class UserForm extends React.Component<UserFormProps, any> {
  // ...
}

const App = Form.create<UserFormProps>({
  // ...
})(UserForm);

此外,每个组件都有"Use with Typescript" section