react-loads icon indicating copy to clipboard operation
react-loads copied to clipboard

Missing type declaration for createResource

Open crymis opened this issue 4 years ago • 3 comments

Correct me, if I'm missing something, but I can not use the useLoads function from a resource I declared.

Example

const partsResource = Loads.createResource<Part[]>({
  _namespace: 'parts',
  load: getParts,
  fooPart: getFooParts,
})

const partsLoader = partsResource.useLoads() // Property 'useLoads' does not exist on type '{ unstable_load: ({ id, args }?: LoadsSuspenderOpts | undefined) => Part[] | undefined; unstable_preload: ({ id, args }?: LoadsSuspenderOpts | undefined) => Part[] | undefined; }'.ts(2339)

Would be great if you could add the missing typings.

crymis avatar Aug 26 '19 12:08 crymis

Thanks for pointing this out. Will work on a fix soon.

jxom avatar Aug 27 '19 00:08 jxom

Is there a fix coming?

Kamahl19 avatar Mar 05 '20 13:03 Kamahl19

I currently use this workaround

type ReactLoadsResource<Response, Err> = {
  useLoads: (
    loadsConfig?: LoadsConfig<Response, Err> | undefined
  ) => {
    isCached: boolean;
    isIdle: boolean;
    isPending: boolean;
    isPendingSlow: boolean;
    isResolved: boolean;
    isRejected: boolean;
    isReloading: boolean;
    isReloadingSlow: boolean;
    load: (..._args: any) => Promise<void | Response> | undefined;
    update:
      | ((..._args: any) => Promise<void | Response> | undefined)
      | ((..._args: any) => Promise<void | Response> | undefined)[]
      | undefined;
    reset: () => void;
    response: Response | undefined;
    error: Err | undefined;
    state: LoadingState;
  };
};

const resource = createResource<Data, Error>({
  context: 'data',
  fn: getData,
}) as ReactLoadsResource<Data, Error>;

Kamahl19 avatar Mar 05 '20 14:03 Kamahl19