Skip to content

React's StatelessComponent results in error #7468

Closed

Description

@pdeva

If you create a stateless element like:

var Hi:React.StatelessComponent<{}> = 
          (props)=><h1>User info</h1>;

Everything is fine so far.
But when you try to use it inside another component like:

var Hello:React.StatelessComponent<{}> = 
          (props)=> <div><Hi></div>;

you will get an error (where you tried to use the Hi component):

Error:(9, 3) TS2605: 
JSX element type 'ReactElement<any>' is not a constructor function for JSX elements.
Property 'render' is missing in type 'ReactElement<any>'.

Activity

  1. pdeva commented on Jan 3, 2016

    @pdeva
    ContributorAuthor

    pinging @vsiao since he seems to be the one who added the type definition for StatelessComponent

  2. vsiao commented on Jan 4, 2016

    @vsiao
    Contributor

    Sorry, I don't use JSX/TSX. Maybe @RyanCavanaugh can help you.

  3. tomenden commented on Jan 13, 2016

    @tomenden

    @pdeva did you find a way to solve this issue?

  4. SethDavenport commented on Feb 21, 2016

    @SethDavenport

    @pdeva - I believe this has been fixed in TypeScript 1.8 - they improved their TSX to handle stateless components.

  5. stroborobo commented on Mar 18, 2016

    @stroborobo

    Right, that's fixed in 1.8:

    microsoft/TypeScript#5675

    There's still a problem with children though:

    const Foobar = ({ children }: { children: ReactNode[] }) => <div>{children}</div>
    error TS2324: Property 'children' is missing in type 'IntrinsicAttributes & { children: (ReactElement<any> | string | number | {} | (ReactElement<any> ...'.
    

    Could make that optional though, or is there a nicer way?

  6. Strato commented on Apr 14, 2016

    @Strato
    Contributor

    I bumped into the children error and it's annoying. Does anyone know how I can circumvent it until it's being fixed?

    I've been looking for a way to cast a JSX component to any, to no avail.

    Something like:

    <Parent as any>
        <Children/>
    </Parent>
    
  7. stroborobo commented on Apr 14, 2016

    @stroborobo

    @Strato What about optionals as I suggested? Better than casting stuff to any. You know, like this:

    const Foobar = ({ children }: { children?: ReactNode[] }) => <div>{children}</div>
  8. Strato commented on Apr 14, 2016

    @Strato
    Contributor

    I didn't understand this was possible. Thank you.

    Hope it gets fixed soon in the definition, as it's a pretty common pattern.

  9. orta commented on Jun 7, 2021

    @orta
    Collaborator

    Hi thread, we're moving DefinitelyTyped to use GitHub Discussions for conversations the @types modules in DefinitelyTyped.

    To help with the transition, we're closing all issues which haven't had activity in the last 6 months, which includes this issue. If you think closing this issue is a mistake, please pop into the TypeScript Community Discord and mention the issue in the definitely-typed channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions