Skip to content

Option to give useAsync the parameter types #333

@dcodesdev

Description

@dcodesdev

Right now all of the custom argument types that can be given to the promiseFn property are type of any.

Meaning when someone else tries to re-use your code, they can not know what types they should provide.

Example:

export const useJoinWaitlist = () => {
  return useAsync({
    promiseFn: ({ email }) =>
      getClient()
        .post<JoinWaitlistResponse>("/waitlist/join", { email })
        .then((r) => r.data),
  })
}

In this case, the type of email is of any

Suggetion

It would be nice to have the option to provide the argument types of the promiseFn

export const useJoinWaitlist = () => {
  type Args = {
    email: string
  }

  return useAsync<JoinWaitlistResponse, Args>({
    promiseFn: ({
      // Email type is now defined and is of type `string`
      email,
    }) =>
      getClient()
        .post<JoinWaitlistResponse>("/waitlist/join", { email })
        .then((r) => r.data),
  })
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions