diff --git a/src/useAsync/useAsync.ts b/src/useAsync/useAsync.ts index 5c68b58cc..2afbab294 100644 --- a/src/useAsync/useAsync.ts +++ b/src/useAsync/useAsync.ts @@ -1,5 +1,6 @@ import { useEffect, useMemo, useRef } from 'react'; import { useSafeState, useFirstMountState, useSyncedRef } from '..'; +import { PartialRequired } from '../util/misc'; export type IAsyncStatus = 'loading' | 'success' | 'error' | 'not-executed'; @@ -7,7 +8,7 @@ export type IAsyncState = | { status: 'not-executed'; error: undefined; - result: Result | undefined; + result: Result; } | { status: 'success'; @@ -17,12 +18,12 @@ export type IAsyncState = | { status: 'error'; error: Error; - result: Result | undefined; + result: Result; } | { status: IAsyncStatus; error: Error | undefined; - result: Result | undefined; + result: Result; }; export interface IUseAsyncOptions { @@ -63,6 +64,17 @@ export interface IUseAsyncMeta { lastArgs: Args | undefined; } +export function useAsync( + asyncFn: (...params: Args) => Promise, + args: Args, + options: PartialRequired, 'initialValue'> +): [IAsyncState, IUseAsyncActions, IUseAsyncMeta]; +export function useAsync( + asyncFn: (...params: Args) => Promise, + args: Args, + options?: IUseAsyncOptions +): [IAsyncState, IUseAsyncActions, IUseAsyncMeta]; + /** * Executes provided async function and tracks its result and error. * @@ -75,8 +87,8 @@ export function useAsync( asyncFn: (...params: Args) => Promise, args: Args, options?: IUseAsyncOptions -): [IAsyncState, IUseAsyncActions, IUseAsyncMeta] { - const [state, setState] = useSafeState>({ +): [IAsyncState, IUseAsyncActions, IUseAsyncMeta] { + const [state, setState] = useSafeState>({ status: 'not-executed', error: undefined, result: options?.initialValue, diff --git a/src/util/misc.ts b/src/util/misc.ts index bc0bccec6..1e31e700e 100644 --- a/src/util/misc.ts +++ b/src/util/misc.ts @@ -17,3 +17,6 @@ export function off( obj.removeEventListener(...(args as Parameters)); } } + +// eslint-disable-next-line @typescript-eslint/naming-convention +export type PartialRequired = Omit & Required>;