[Vuejs]-How to wrap a promise and keep the return type?

1👍

You can use a type assertion for the makeRequest function.

export function useForm<
  T extends Record<string, any>, 
  R extends (...args: any) => Promise<AxiosResponse>
>(init: T, request: R) {

  /* ... */  

  const makeRequest = (() => {
    /* ... */
  }) as () => ReturnType<R>

  /* ... */
}

Now the returned makeRequest function will have the correct type.

const {form, isLoading, errors, makeRequest} = useForm({
  username: null,
  password: null,
  password_confirmation: null,
  referral_code: null
}, register)

const result = makeRequest()
//     ^? const result: Promise<AxiosResponse<UserResponse, any>>

Playground

Leave a comment