fix(Button): narrow ButtonProps children to ReactNode#4033
Open
patrickwehbe wants to merge 1 commit into
Open
Conversation
ButtonProps extends PressableProps, whose children type under
@types/react 19 is ReactNode | ((state: PressableStateCallbackType) => ReactNode).
React 18+ no longer accepts function children as ReactNode in JSX, so any
wrapper that spreads ButtonProps via {...rest} into <Button /> fails to
type-check with TS2322.
The Button component only ever renders children as nodes (it defaults
children to title and runs them through React.Children.toArray); it never
calls the render-prop form. Declaring children?: ReactNode on ButtonProps
narrows the inherited type to what the component actually supports.
Closes react-native-elements#4032
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
ButtonPropsextendsPressableProps, which typeschildrenasReactNode | ((state: PressableStateCallbackType) => ReactNode). Since React 18,@types/reactno longer accepts function children as aReactNodein JSX, so any wrapper that spreadsButtonPropsvia{...rest}into<Button />fails to type-check:The
Buttoncomponent never uses the render-prop form ofchildren. It defaultschildrentotitleand renders whatever it gets throughReact.Children.toArray, so the function variant is dead from the public API's point of view. NarrowingchildrentoReactNodeonButtonPropsdrops the variant the component doesn't support and lets these wrappers compile again.This is a type-only change, no runtime behavior changes.
Fixes #4032
Type of change
How Has This Been Tested?
Reproduced the error first, then confirmed the fix, with the versions already pinned in the repo (TypeScript 5.9,
@types/react19). A wrapper that does<Button {...rest} />reports TS2322 onnextand type-checks clean with this change.yarn typescriptpasses.Checklist