MFB-Technologies-Inc/react-async-renderer

calculate the async renderer's return type from createAsyncRenderer's arguments

lemke-ethan opened this issue · 1 comments

the returned function, namely the async renderer, from createAsyncRenderer doesn't always have to be possibly null. it would be nice if the return type of the async renderer was determined by the arguments of createAsyncRenderer, such that if none of the arguments show the potential for a null return value then null should not be a possible return value for the async renderer.

maybe something like this:

declare function createAsyncRenderer<
  T extends OnCompletedSuccessfullyArgs = OnCompletedSuccessfullyWithoutArgs
>(
  args: CreateAsyncRendererArgs<T>
): <TSuccess extends OnCompletedSuccessfully<T>, TError extends JSX.Element | React.FC<{errorMEssage?:string}> | undefined = undefined>(
  onCompletedSuccessfully: TSuccess,
  optionalArgs?: {
    onCompletedWithError?: TError
    onLoading?: JSX.Element | (() => JSX.Element)
    onInit?: JSX.Element | (() => JSX.Element)
  }
) => TSuccess extends JSX.Element | (()=>JSX.Element) ? TError extends JSX.Element | (()=>JSX.Element) | undefined ? ReactElement<any,any> : ReactElement<any, any> | null : ReactElement<any, any> | null

Here's a playground showing it working for some cases; not fully tested.