简体   繁体   English

打字稿错误:TS2604 - 如何修复?

[英]Typescript Error: TS2604 - How to fix?

I am trying to use a library called redux-form in a Typescript based project.我正在尝试在基于 Typescript 的项目中使用名为 redux-form 的库。 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>
        );
    }
}

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. 希望这可以帮助。

I can't comment on the answer above (from Amid), because I don't have enough reputation on StackOverflow, but his answer is correct. 我无法对上述答案发表评论(来自Amid),因为我在StackOverflow上没有足够的声誉,但是他的答案是正确的。

I had the same issue: * (82,14): error TS2604: JSX element type 'SlideForm' does not have any construct or call signatures.* 我遇到了同样的问题: *(82,14):错误TS2604:JSX元素类型'SlideForm'没有任何构造或调用签名。*

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 you. 这导致我遇到了与您相同的问题。


So I change the way I'm doing the import to this : 所以我改变了导入的方式:

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

And it solves my issue. 它解决了我的问题。

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/..'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动态导入ES6 React组件并出现Typescript TS2604错误? - Dynamic Import ES6 React Component with Typescript TS2604 error? 打字稿TS2604:JSX元素类型“抽屉”没有任何构造或调用签名 - Typescript TS2604: JSX element type 'Drawer' does not have any construct or call signatures 打字稿类型定义:TS2604:JSX元素类型“某物”没有任何构造或调用签名 - Typescript type definition: TS2604: JSX element type 'something' does not have any construct or call signatures 错误TS2604:JSX元素类型“ ...”没有任何构造或调用签名 - Error TS2604: JSX element type '…' does not have any construct or call signatures 当组件作为prop传递时,我尝试渲染时得到TS2604错误 - When passing component as prop i get TS2604 error when i try and render it 错误 TS2604:JSX 元素类型“轮播”没有任何构造或调用签名 - error TS2604: JSX element type 'Carousel' does not have any construct or call signatures TS2604:JSX 元素类型“标头”没有任何构造或调用签名 - TS2604: JSX element type 'Header' does not have any construct or call signature TS2604:将“ material-ui”按钮从一个包导入另一个包时 - TS2604: When importing 'material-ui' button from one package to another TS2604:JSX元素类型“没有任何构造或调用签名 - TS2604: JSX elemy type '' does not have any construct or call signatures 是什么原因导致 typescript Module has no exported member.ts(2305) 错误,如何解决? - What causes the typescript Module has no exported member .ts(2305) error and how do you fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM