TypeScript, redux-form and connect

2019-04-10 08:24发布

问题:

First of all, I'm very new to TS, React & Redux, so sorry if this is an obvious question.

I'm trying to modify this example to get a form to load some information. It's using redux-form.

I'm trying to figure out how to call connect and redux-form at the same time in the export of the component. Right now it's looking like this:

class UserForm extends React.Component<IUserProps, void> { .. }

export default ReduxForm.reduxForm({
  form: 'user',
  fields: [
    'userName',
    'password',
    'firstName',
    'lastName',
    'email'
  ],
  validate: UserForm.validate,
})(UserForm);

The examples I've seen without TS look like this:

class MyForm extends Component {}
MyForm = reduxForm(config)(MyForm)
MyForm = connect(mapStateToProps, mapDispatchToProps)(MyForm)
export default MyForm

But if I try to do the same in TS I get the TS2300 error: duplicate identifier.

I've also tried to use the @connect decorator, but I couldn't make it work (or find any working example online).

回答1:

Can you just chain them?

class UserForm extends React.Component<IUserProps, void> { .. }

export default connect(mapStateToProps,mapDispatchToProps) 
    (ReduxForm.reduxForm({
  form: 'user',
  fields: [
    'userName',
    'password',
    'firstName',
    'lastName',
    'email'
  ],
  validate: UserForm.validate,
})(UserForm));


回答2:

not a fix, but issue can be silenced by casting to any

connect(mapStateToProps, mapDispatchToProps)(MyForm as any)