diff --git a/types/react-dom/canary.d.ts b/types/react-dom/canary.d.ts index a2e9885b0b1299..73e71c7daa4a22 100644 --- a/types/react-dom/canary.d.ts +++ b/types/react-dom/canary.d.ts @@ -145,8 +145,35 @@ declare module "./client" { [REACT_FORM_STATE_SIGIL]: never; } + interface RootOptions { + onUncaughtError?: + | ((error: unknown, errorInfo: { componentStack?: string | undefined }) => void) + | undefined; + onCaughtError?: + | (( + error: unknown, + errorInfo: { + componentStack?: string | undefined; + errorBoundary?: React.Component | undefined; + }, + ) => void) + | undefined; + } + interface HydrationOptions { formState?: ReactFormState | null; + onUncaughtError?: + | ((error: unknown, errorInfo: { componentStack?: string | undefined }) => void) + | undefined; + onCaughtError?: + | (( + error: unknown, + errorInfo: { + componentStack?: string | undefined; + errorBoundary?: React.Component | undefined; + }, + ) => void) + | undefined; } interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_CREATE_ROOT_CONTAINERS { diff --git a/types/react-dom/test/canary-tests.tsx b/types/react-dom/test/canary-tests.tsx index 7f27f59dabf6fd..d8d3b845b570c0 100644 --- a/types/react-dom/test/canary-tests.tsx +++ b/types/react-dom/test/canary-tests.tsx @@ -265,4 +265,50 @@ function formTest() { function createRoot(validContainer: Element | DocumentFragment | Document) { ReactDOMClient.createRoot(document); ReactDOMClient.createRoot(validContainer); + + ReactDOMClient.createRoot(document, { + onUncaughtError: (error, errorInfo) => { + // $ExpectType unknown + error; + // $ExpectType string | undefined + errorInfo.componentStack; + // @ts-expect-error -- only on onRecoverableError + errorInfo.digest; + // @ts-expect-error -- only on onCaughtError + errorInfo.errorBoundary; + }, + onCaughtError: (error, errorInfo) => { + // $ExpectType unknown + error; + // $ExpectType string | undefined + errorInfo.componentStack; + // @ts-expect-error -- only on onRecoverableError + errorInfo.digest; + // $ExpectType Component | undefined + errorInfo.errorBoundary; + }, + }); + + ReactDOMClient.hydrateRoot(document.body, null, { + onUncaughtError: (error, errorInfo) => { + // $ExpectType unknown + error; + // $ExpectType string | undefined + errorInfo.componentStack; + // @ts-expect-error -- only on onRecoverableError + errorInfo.digest; + // @ts-expect-error -- only on onCaughtError + errorInfo.errorBoundary; + }, + onCaughtError: (error, errorInfo) => { + // $ExpectType unknown + error; + // $ExpectType string | undefined + errorInfo.componentStack; + // @ts-expect-error -- only on onRecoverableError + errorInfo.digest; + // $ExpectType Component | undefined + errorInfo.errorBoundary; + }, + }); }