Skip to content

Commit 04eb8af

Browse files
authored
fix(useErrorBoundary): state type narrow (#161)
1 parent 8b2c6c5 commit 04eb8af

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/useErrorBoundary.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ import { useContext, useMemo, useState } from "react";
22
import { assertErrorBoundaryContext } from "./assertErrorBoundaryContext";
33
import { ErrorBoundaryContext } from "./ErrorBoundaryContext";
44

5-
export type UseErrorBoundaryApi<Error> = {
5+
type UseErrorBoundaryState<TError> =
6+
| { error: TError; hasError: true }
7+
| { error: null; hasError: false };
8+
9+
export type UseErrorBoundaryApi<TError> = {
610
resetBoundary: () => void;
7-
showBoundary: (error: Error) => void;
11+
showBoundary: (error: TError) => void;
812
};
913

10-
export function useErrorBoundary<Error = any>(): UseErrorBoundaryApi<Error> {
14+
export function useErrorBoundary<TError = any>(): UseErrorBoundaryApi<TError> {
1115
const context = useContext(ErrorBoundaryContext);
1216

1317
assertErrorBoundaryContext(context);
1418

15-
const [state, setState] = useState<{
16-
error: Error | null;
17-
hasError: boolean;
18-
}>({
19+
const [state, setState] = useState<UseErrorBoundaryState<TError>>({
1920
error: null,
2021
hasError: false,
2122
});
@@ -26,7 +27,7 @@ export function useErrorBoundary<Error = any>(): UseErrorBoundaryApi<Error> {
2627
context?.resetErrorBoundary();
2728
setState({ error: null, hasError: false });
2829
},
29-
showBoundary: (error: Error) =>
30+
showBoundary: (error: TError) =>
3031
setState({
3132
error,
3233
hasError: true,

0 commit comments

Comments
 (0)