question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript typings for render(props.children) should be ComponentChild[], not ComponentChildren

See original GitHub issue

Since preact makes the very useful guarantee that, in the render function, props.children will always be an array, typing the props as RenderableProps<P> gives the overly-broad type for the children property of ComponentChildren. This used to be just ComponentChild[], but has now been extended significantly (I believe to improve tsx support).

For example:

render(props:RenderableProps<MyProps>) {
   if(props.children.length) {  <= FAIL

You have to do the rather ugly:

render(props:RenderableProps<MyProps>) {
    if((props.children as ComponentChild[]).length) {

instead, to encode the assertion that we know that .children is guaranteed to be an array in the render function.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Alexendoocommented, Oct 4, 2018

Oh is that to say a TS change wouldn’t be needed in that case @marvinhagemeister? That is kind of relieving since I was finding it difficult to model the preact behaviour.

I think I agree with you on the children front too, in practise most components I write rarely have an opinion about the type of children passed to it

1reaction
timpurcommented, Sep 8, 2018

+ 1

I tried this and had issues using ComponentChild[] but think your on the right idea

Also to add shouldn’t render be able to return props.children ? thus render: ComponentChildren

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Children with TypeScript | Building SPAs - Carl Rippon
The React children prop allows components to be composed together and is a key concept for building reusable components. Visually, we can ......
Read more >
How Children Types Work In React 18 And TypeScript 4
We even can have functions as children (aka, render props)! ... In TypeScript, when I'm not sure what type to use, my first...
Read more >
What is the type of the 'children' prop? - Stack Overflow
I console logged and can confirm that I have a children prop, but even with <React.Fragment> surrounding {props.children} I am not returning ...
Read more >
What are props in react and how to use them with typescript?
The rendered of this phrase is in the child component Children that it will receive the props value true and will print a...
Read more >
Module: ojvcomponent - Oracle
Both children and slots can be embedded directly in a virtual DOM tree: render(props: Readonly<Props>): ComponentChild { return ( <div> ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found