Skip to content

Commit

Permalink
[react-dom] Add types for onUncaughtError and onCaughtError
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Mar 27, 2024
1 parent 212dfbf commit 47a23eb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
28 changes: 28 additions & 0 deletions types/react-dom/canary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,36 @@ declare module "./client" {
[REACT_FORM_STATE_SIGIL]: never;
}

interface RootOptions {
formState?: ReactFormState | null;
onUncaughtError?:
| ((error: unknown, errorInfo: { componentStack?: string | undefined }) => void)
| undefined;
onCaughtError?:
| ((
error: unknown,
errorInfo: {
componentStack?: string | undefined;
errorBoundary?: React.Component<unknown> | 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<unknown> | undefined;
},
) => void)
| undefined;
}

interface DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_CREATE_ROOT_CONTAINERS {
Expand Down
44 changes: 44 additions & 0 deletions types/react-dom/test/canary-tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,48 @@ 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<unknown, {}, any> | 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;
},
onCaughtError: (error, errorInfo) => {
// $ExpectType unknown
error;
// $ExpectType string | undefined
errorInfo.componentStack;
// @ts-expect-error -- only on onRecoverableError
errorInfo.digest;
// $ExpectType Component<unknown, {}, any> | undefined
errorInfo.errorBoundary;
},
});
}

0 comments on commit 47a23eb

Please sign in to comment.