Skip to content

Commit

Permalink
fix(useErrorBoundary): state type narrow (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
manudeli authored Aug 11, 2023
1 parent 8b2c6c5 commit 04eb8af
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/useErrorBoundary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ import { useContext, useMemo, useState } from "react";
import { assertErrorBoundaryContext } from "./assertErrorBoundaryContext";
import { ErrorBoundaryContext } from "./ErrorBoundaryContext";

export type UseErrorBoundaryApi<Error> = {
type UseErrorBoundaryState<TError> =
| { error: TError; hasError: true }
| { error: null; hasError: false };

export type UseErrorBoundaryApi<TError> = {
resetBoundary: () => void;
showBoundary: (error: Error) => void;
showBoundary: (error: TError) => void;
};

export function useErrorBoundary<Error = any>(): UseErrorBoundaryApi<Error> {
export function useErrorBoundary<TError = any>(): UseErrorBoundaryApi<TError> {
const context = useContext(ErrorBoundaryContext);

assertErrorBoundaryContext(context);

const [state, setState] = useState<{
error: Error | null;
hasError: boolean;
}>({
const [state, setState] = useState<UseErrorBoundaryState<TError>>({
error: null,
hasError: false,
});
Expand All @@ -26,7 +27,7 @@ export function useErrorBoundary<Error = any>(): UseErrorBoundaryApi<Error> {
context?.resetErrorBoundary();
setState({ error: null, hasError: false });
},
showBoundary: (error: Error) =>
showBoundary: (error: TError) =>
setState({
error,
hasError: true,
Expand Down

0 comments on commit 04eb8af

Please sign in to comment.