preact
preact copied to clipboard
Allow cloneElement to create copies of `ComponentChild`
What am I trying to achieve?
Fixes https://github.com/preactjs/preact/issues/2428
Update typings for cloneElement
to create copies of ComponentChild
, namely VNode
and primitives.
What approach did I use?
Most of these changes involve type definitions. Given ComponentChild
can be:
type ComponentChild = VNode<any> | object | string | number | boolean | null | undefined;
- We overload
cloneElement
's function signature in acceptComponentChild
- If the child is singular and not a VNode, we simple return create and return its copy.
On a side note, React's ReactChild is defined as follows:
type ReactChild = ReactElement | ReactText;
. cloneElement
should work with string
and number
at the very least.
Also, I think this still feels a bit weird — I'm wondering if using type inference / casting as mentioned in the issue should be enough to fix this issue.