You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.
This comment claims this use case is pretty rare, but I'm either ignorant of the alternatives, or I'm not convinced. Anywhere one wants to have a tree-like component structure, one potentially butts up against this limitation. Consider a structure involving Nodes and Leafs as follows:
So far, a Node is merely a list of Leafs. Now let's say we want to augment our Node to contain zero or more entries, each of which may either be a Node or Leaf. How is this done? This won't work:
…functionrecursive(...args: any){// 7023: 'recursive' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.returnPropTypes.arrayOf(PropTypes.oneOfType([LeafComponent.propTypes,PropTypes.shape({// 2615: Type of property 'children' circularly references itself in mapped type '{ children: never; }'.children: recursive,}).isRequired,]))(...args)// 2556: Expected 5 arguments, but got 0 or more.}functionNodeComponent({ children }: NodeComponentT){return(<ul>{children.map((child,_)=>{if(childinstanceofObject&&"children"inchild){constnode_child=childasNodeComponentTreturn(<li><NodeComponentchildren={node_child.children}/></li>)}else{constleaf_child=childasLeafComponentTreturn(<LeafComponentkey={leaf_child.id}{...leaf_child}/>)}})}</ul>)}NodeComponent.propTypes={children: PropTypes.arrayOf(recursive).isRequired,}typeNodeComponentT=InferProps<typeofNodeComponent.propTypes>…
You'll get a circular reference type error. So how should one handle this case in the real world? I can find near zero documentation on how to do this, either here or via the React docs. Any guidance would be appreciated.
The text was updated successfully, but these errors were encountered:
To clarify, what I'm pointing out by filing this issue is that no clear guidance exists to address this pattern. I've clearly gotten it wrong here, but that's largely because I've been feeling around in the dark absent such guidance.
You'd handle this by using a custom propType to wrap the recursion, rather than actually using recursion.
☝️ Thanks, but I don't understand what that means or whether it differs from your prior recommendation on #246. Can you point to a clear example?
Right now, I've constructed the following work-around:
typeLeafComponentT=InferProps<typeofLeafComponent.propTypes>functionLeafComponent({ id, name }: LeafComponentT){return(<span>{name} ({id})</span>)}LeafComponent.propTypes={id: PropTypes.string.isRequired,name: PropTypes.string.isRequired,}interfaceRootComponentT{children: NodeComponentT[],}functionRootComponent({ children }: RootComponentT){if(children&&children.length>0){return(<ul>{children.map((child,_)=>(<NodeComponent{...child}/>))}</ul>)}else{returnnull}}RootComponent.propTypes={children: PropTypes.array.isRequired,}typeNodeComponentT=RootComponentT&{leaf: InferProps<typeofLeafComponent.propTypes>,}functionNodeComponent({ leaf, children }: NodeComponentT){return(<li><LeafComponentkey={leaf.id}{...leaf}/><RootComponentchildren={children}/></li>)}NodeComponent.propTypes={leaf: PropTypes.shape(LeafComponent.propTypes).isRequired,children: PropTypes.array.isRequired,}
I call this a work-around because there is no runtime type checking of the elements of children.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
This comment claims this use case is pretty rare, but I'm either ignorant of the alternatives, or I'm not convinced. Anywhere one wants to have a tree-like component structure, one potentially butts up against this limitation. Consider a structure involving
Node
s andLeaf
s as follows:So far, a
Node
is merely a list ofLeaf
s. Now let's say we want to augment ourNode
to contain zero or more entries, each of which may either be aNode
orLeaf
. How is this done? This won't work:You'll get a circular reference type error. So how should one handle this case in the real world? I can find near zero documentation on how to do this, either here or via the React docs. Any guidance would be appreciated.
The text was updated successfully, but these errors were encountered: