Skip to content

Infer the wrapped component's ref type in withErrorBoundary - #249

Merged
bvaughn merged 2 commits into
bvaughn:mainfrom
Alberto-BaseNet:fix-with-error-boundary-ref-type
Sep 5, 2026
Merged

Infer the wrapped component's ref type in withErrorBoundary#249
bvaughn merged 2 commits into
bvaughn:mainfrom
Alberto-BaseNet:fix-with-error-boundary-ref-type

Conversation

@Alberto-BaseNet

Copy link
Copy Markdown
Contributor

Fixes #248.

withErrorBoundary was parameterized over Type extends ComponentClass<unknown>, but no parameter mentions Type, so it can never be inferred and always falls back to its constraint. Every wrapper came out typed as taking Ref<Component<unknown, any, any>>, so passing the ref the wrapped component actually takes fails to compile.

This parameterizes over the component instead, so Type is inferred from the argument, and reads both sides off it with ComponentProps / ComponentRef — matching what the runtime already forwards, and what 6.0.0 declared.

The existing ref test passes a class component, whose instance structurally satisfies Component<unknown, any, any>, which is why this survived #211. The added test passes a function component declaring a ref prop — the React 19 form — and does not compile without the change.

The constraint needs any because React's own ComponentProps and ComponentRef are constrained by JSXElementConstructor<any>; there is a comment and a narrow eslint exception on that line.

Motivation: we hit this upgrading a React 19 CRM frontend off 6.0.0, where it breaks 9 call sites and keeps us pinned.

Co-authored-by: Claude Opus 5 noreply@anthropic.com

The HOC was parameterized over `Type extends ComponentClass<unknown>`,
but no argument mentions `Type`, so it can never be inferred and always
falls back to its constraint. Every wrapper therefore came out typed
`RefAttributes<Component<unknown, any, any>>`, and passing the ref the
wrapped component actually takes stops type-checking:

  Type 'RefObject<Handle | null>' is not assignable to type
  'Ref<Component<unknown, any, any>>'

Parameterize over the component instead, so `Type` is inferred from the
argument, and read the props and ref off it with `ComponentProps` and
`ComponentRef` — which is also what the runtime already forwards.

The existing ref test passes a class component, whose instance happens
to satisfy `Component<unknown, any, any>`, which is why this went
unnoticed. The added test passes a function component that declares a
`ref` prop, the React 19 form, and fails to compile without this change.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
@vercel

vercel Bot commented Sep 2, 2026

Copy link
Copy Markdown

@Alberto-BaseNet is attempting to deploy a commit to the Brian Vaughn's projects Team on Vercel.

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Sep 5, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
react-error-boundary Ready Ready Preview Sep 5, 2026 12:50pm UTC

@bvaughn bvaughn left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, Alberto 👍🏼

@bvaughn
bvaughn merged commit 526d032 into bvaughn:main Sep 5, 2026
6 checks passed
@bvaughn

bvaughn commented Sep 5, 2026

Copy link
Copy Markdown
Owner

Published in react-error-boundary@6.1.5

@Alberto-BaseNet
Alberto-BaseNet deleted the fix-with-error-boundary-ref-type branch September 7, 2026 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

withErrorBoundary: ref type collapses to Component<unknown, any, any> since 6.1.0

2 participants