Fix mergeProp, createElemPropsHook, and composeHooks types
#2979
NicholasBoll
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
mergePropsmergePropsis a utility function used by all of our components to properly merge React props fromReact hooks and the props you pass to components. The functionality merges props like the
csprop,cssprop,styleprop, callbacks, and other props in a way that tries to be intuitive. If you adda
csprop and a hook also has acsprop, both are merged in such a way that thecsprop fromboth is represented. Same with the
styleprop - the style objects will be merged together.Callbacks are merged in a way that both callbacks are called. The strange and undocumented merging
was when one of the props is
null. Basically, in the case ofmergeProps(props1, props2),props2will overrideprops1(callbacks are merged so thatprops2callbacks are called afterprops1), except ifprops1sets anullvalue.The
nullprocessing allows behavior hooks to "remove" props from passed props. Usually this is forprop redirection or allow passing non-element props to a hook and the hook can remove it. The
runtime works fine, but TypeScript didn't like how we merged the types:
The problem is if a prop has a
nulland a non-null value:TypeScript Playground
There is also a problem if a prop type was changed:
TypeScript Playground
Since we used
&, an intersection type, there is no intersection betweenstringandnumberandtherefore
foobecomesnever. The runtime actually makesfoobe anumber.Fixing this revealed a common problem - a prop type may require a narrowed type, but a hook provides a widened type. This required us to add
as constto our props:We use the TypeScript 5.0 const Type Parameters to narrow the type. The object passed is a constant, so we upgraded
mergePropsandcreateElemPropsHookto use const Type Parameters to automatically narrow the type to avoid this issue.We upgraded the type of
mergePropsto be more complicated, but more accurate so that prop types override prop types and also thenullhandle works:createElemPropsHookThe
createElemPropsHooknow uses const Type Parameters to narrow types. Similar tomergeProps:We use const Type Parameters to narrow the type since the elemProps hook returns a read-only object. This prevents you from needing to provide
as const.composeHookscomposeHookscomposes multiple hooks together, callingmergePropsalong the way. If we used anullin any hook the result was returningnever. We updated to accurately return the correct type. This could be alerting you to errors you didn't see before since the spreadelemPropsdidn't have props before and thus wouldn't have any conflicts.Beta Was this translation helpful? Give feedback.
All reactions