diff --git a/public/app/features/alerting/unified/hooks/useAsync.tsx b/public/app/features/alerting/unified/hooks/useAsync.tsx index 96b5a7bfcd2..66790ed348b 100644 --- a/public/app/features/alerting/unified/hooks/useAsync.tsx +++ b/public/app/features/alerting/unified/hooks/useAsync.tsx @@ -8,26 +8,34 @@ import { stringifyErrorLike } from '../utils/misc'; export type AsyncStatus = 'loading' | 'success' | 'error' | 'not-executed'; export type AsyncState = - | { - status: 'not-executed'; - error: undefined; - result: Result; - } - | { - status: 'success'; - error: undefined; - result: Result; - } - | { - status: 'error'; - error: Error; - result: Result; - } - | { - status: AsyncStatus; - error: Error | undefined; - result: Result; - }; + | AsyncStateUninitialized + | AsyncStateFulfilled + | AsyncStateWithError + | AsyncStateLoading; + +export type AsyncStateWithError = { + status: 'error'; + error: Error; + result: Result; +}; + +export type AsyncStateFulfilled = { + status: 'success'; + error: undefined; + result: Result; +}; + +export type AsyncStateUninitialized = { + status: 'not-executed'; + error: undefined; + result: Result; +}; + +export type AsyncStateLoading = { + status: 'loading'; + error: Error | undefined; + result: Result; +}; export type UseAsyncActions = { /** @@ -157,19 +165,19 @@ function useSyncedRef(value: T): { readonly current: T } { // --- utility functions to help with request state assertions --- -export function isError(state: AsyncState) { +export function isError(state: AsyncState): state is AsyncStateWithError { return state.status === 'error'; } -export function isSuccess(state: AsyncState) { +export function isSuccess(state: AsyncState): state is AsyncStateFulfilled { return state.status === 'success'; } -export function isUninitialized(state: AsyncState) { +export function isUninitialized(state: AsyncState): state is AsyncStateUninitialized { return state.status === 'not-executed'; } -export function isLoading(state: AsyncState) { +export function isLoading(state: AsyncState): state is AsyncStateLoading { return state.status === 'loading'; } @@ -185,7 +193,7 @@ export function anyOfRequestState(...states: Array>) { /** * This is only used for testing and serializing the async state */ -export function SerializeState({ state }: { state: AsyncState }) { +export function SerializeState({ state }: { state: AsyncState }) { return ( <> {isUninitialized(state) && 'uninitialized'}