Skip to content

JSX: Typing both stateless component and component class in react component property gives error #8657

@asvetliakov

Description

@asvetliakov

I need to type both stateless component and classic component in single component property:

TypeScript Version:
1.8.10/1.9.0-dev.20160518-1.0

Code

// A self-contained demonstration of the problem follows...
// Ideally:
interface ComponentProps {
   ButtonComponent: React.StatelessComponent<any> | React.ComponentClass<any>;
  ...
}

class MyComponent extends React.Component<ComponentProps, {}> {
  render() {
    const { ButtonComponent } = this.props;
    const someProps = {};
    const button = <ButtonComponent {...someProps}/>
    return (<div>{button}</div>);
  }
}

// Ideal usage

<MyComponent ButtonComponent={() => <button>test</button>}/>

class MyButtonComponent extends React.Component<{},{}> {
...
}

<MyComponent ButtonComponent={MyButtonComponent} />

Expected behavior:
Compiles successfully

Actual behavior:
Throws error: "JSX Element type ButtonComponent doesn't have any construct or call signatures"
It works If i change definition of ButtonComponent to just solo React.StatelessComponent or
React.ComponentClass

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions