Skip to content

The ResourceActions type is broken #2507

@jb-1980

Description

@jb-1980

Describe the bug

Currently, ResourceActions is typed as

export type ResourceActions<T, R = unknown> = {
  mutate: Setter<T>;
  refetch: (info?: R) => T | Promise<T> | undefined | null;
};

This means refetch could be:

  • A function that takes info?: R and returns T, OR
  • A Promise<T>, OR
  • undefined, OR
  • null

But looking at the actual implementation, refetch is always a function:

{ refetch: info => runWithOwner(owner, () => load(info)), mutate: setValue }

I believe the intention with ResourceActions was:

export type ResourceActions<T, R = unknown> = {
  mutate: Setter<T>;
  refetch: (info?: R) => (T | Promise<T> | undefined | null); // needed parentheses
};

Now it correctly indicates that refetch is a function that takes info?: R and will return T | Promise<T> | undefined | null

Your Example Website or App

https://playground.solidjs.com/anonymous/e5e44212-244e-41de-bd64-db7247e8fefe

Steps to Reproduce the Bug or Issue

  1. Create a resource:
const [data, {refetch}] = createResource(fn)
  1. Use the refetch as a Promise in some update function:
const updateData = () => refetch.catch(logError)
  1. Get bug undefined has no property catch

Expected behavior

I expect that my IDE and linter can correctly tell me that I am using refetch the right way, without having to test if it is a promise, or undefined, or null.

Screenshots or Videos

No response

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. 91.1]

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions