TypeScript typings for render(props.children) should be ComponentChild[], not ComponentChildren
See original GitHub issueSince 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:
- Created 5 years ago
- Reactions:3
- Comments:9 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
+ 1
I tried this and had issues using
ComponentChild[]
but think your on the right ideaAlso to add shouldn’t
render
be able to returnprops.children
? thusrender: ComponentChildren