我正在使用 React、Redux、Redux-Form 和 TypeScript 开发应用程序。我正在努力使用包@types/redux-form(绝对类型)定义的类型,尤其是使用 FieldArray。有一个属性“组件”,我在其中传递了对组件的引用,但是我不知道在描述它的props时应该使用什么类型。我从Redux-Form 存储库下载了使用 FieldArray 的示例,并稍微修改了它以使用 TypeScript。我想用 compilerOption "noImplicitAny": true 编译它。所以这里是 FieldArraysForm.tsx(类似于官方示例):
import * as React from 'react';
import { Field, FieldArray, reduxForm } from 'redux-form';
import validate from './validate';
const renderField = (props) => {
const { touched, error } = props.meta;
const { input, label, type } = props;
return (
<div>
<label>{label}</label>
<div>
<input {...input} type={type} placeholder={label} />
{touched && error && <span>{error}</span>}
</div>
</div>
)
}
const renderHobbies = (props) => {
const { fields } = props;
const { error } = props.meta;
return (
<ul>
<li>
<button type="button" onClick={() => fields.push()}>
Add Hobby
</button>
</li>
{fields.map((hobby, index) => (
<li key={index}>
<button
type="button"
title="Remove Hobby"
onClick={() => fields.remove(index)}
/>
<Field
name={hobby}
type="text"
component={renderField}
label={`Hobby #${index + 1}`}
/>
</li>
))}
{error && <li className="error">{error}</li>}
</ul>
)
}
const renderMembers = (props) => {
const { fields } = props;
const { error, submitFailed } = props.meta;
return (
<ul>
<li>
<button type="button" onClick={() => fields.push({})}>
Add Member
</button>
{submitFailed && error && <span>{error}</span>}
</li>
{fields.map((member, index) => (
<li key={index}>
<button
type="button"
title="Remove Member"
onClick={() => fields.remove(index)}
/>
<h4>Member #{index + 1}</h4>
<Field
name={`${member}.firstName`}
type="text"
component={renderField}
label="First Name"
/>
<Field
name={`${member}.lastName`}
type="text"
component={renderField}
label="Last Name"
/>
<FieldArray name={`${member}.hobbies`} component={renderHobbies} />
</li>
))}
</ul>
)
}
const FieldArraysForm = props => {
const { handleSubmit, pristine, reset, submitting } = props
return (
<form onSubmit={handleSubmit}>
<Field
name="clubName"
type="text"
component={renderField}
label="Club Name"
/>
<FieldArray name="members" component={renderMembers} />
<div>
<button type="submit" disabled={submitting}>
Submit
</button>
<button type="button" disabled={pristine || submitting} onClick={reset}>
Clear Values
</button>
</div>
</form>
)
}
export default reduxForm({
form: 'fieldArrays', // a unique identifier for this form
validate
})(FieldArraysForm)
我如何使它与类型一起工作?