preact icon indicating copy to clipboard operation
preact copied to clipboard

Allow cloneElement to create copies of `ComponentChild`

Open ddayguerrero opened this issue 4 years ago • 1 comments

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 accept ComponentChild
  • 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.

ddayguerrero avatar May 14 '20 02:05 ddayguerrero

Coverage Status

Coverage decreased (-0.2%) to 99.597% when pulling 45c8bcca431cf7c7261ef33976ba49c87a4e310e on ddayguerrero:expand-types-clone-element into da382e13d9377a53056e4cb0fd741f6e0aadf1c1 on preactjs:master.

coveralls avatar May 14 '20 03:05 coveralls