Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Error: TS2604 - How to fix?

I am trying to use a library called redux-form in a Typescript based project. When I take their "simple form" example code and implement it I get an error:

error TS2604: JSX element type 'SimpleForm' does not have any construct or call signatures.

What am I doing wrong? I have the definition file for this lib installed so either I have coded it incorrectly or the definition file is incorrect.

In my form component (stripped the code way down to make it as lean as I can):

import * as React from 'react';
import {reduxForm} from 'redux-form';
export const fields = ['firstName'];

class SimpleForm extends React.Component<any, any> {

  static propTypes = {
    fields: React.PropTypes.object.isRequired,
  };

  render() {
    const {
      fields: {firstName}
      } = this.props;
    return (<form>
        <div>
          <label>First Name</label>
          <div>
            <input type="text" placeholder="First Name" {...firstName}/>
          </div>
        </div>

        <div>
          <button>
            Submit
          </button>
        </div>
      </form>
    );
  }
}

export default reduxForm({
  form: 'simple',
  fields
})(SimpleForm);

and I consume it here (which results in the above error):

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as SimpleForm from '../components/GuestForm';

export default class Main extends React.Component<any, any> {
     render() {
        return (
        <div className='mainApp'>
            <SimpleForm />
        </div>
        );
    }
}
like image 200
rg88 Avatar asked Jan 24 '16 01:01

rg88


3 Answers

In your case you can also do this (since it has been exported default)

import * as SlideForm from '../../common/SliderForm';

This error occurs if the component has been exported as the default and you're trying to import it with {} or as *

OR the other way around.

If the component has not been declared as default, it should be imported with braces for example -

import {component} from '../path/..'
like image 89
harp3048 Avatar answered Oct 15 '22 23:10

harp3048


I think your problem is in how you import default exported class:

export default reduxForm(...

If you have it declared as above then you should import it like this:

import SimpleForm from '../components/GuestForm'

Otherwise you can remove 'default' and import it like this:

import {SimpleForm} from '../components/GuestForm' 

Hope this helps.

like image 20
Amid Avatar answered Oct 15 '22 23:10

Amid


I can't comment on the answer above (from Amid), because I don't have enough reputation on StackOverflow, but his answer is correct.

I had the same issue: *** (82,14): error TS2604: JSX element type 'SlideForm' does not have any constructor call signatures.***

Here is my previous code:

export default class SliderForm extends React.Component<any, any> {...};

with the previous way of importing the class :

import * from SlideForm from '../../common/SliderForm';

This leads me to the same issue as yours.


So I changed the way I'm doing the import to this :

import SlideForm from '../../common/SliderForm';

And it solved my issue.

like image 5
Amélie Medem Avatar answered Oct 15 '22 23:10

Amélie Medem