0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00
astro/.changeset/small-vans-own.md
Ben Holmes ea4bc04e94
feat: ActionReturnType (#11443)
* feat: ActionReturnType util

* feat(test): ActionReturnType

* chore: changeset
2024-07-10 07:05:13 -04:00

705 B

astro
patch

Expose new ActionReturnType utility from astro:actions. This infers the return type of an action by passing typeof actions.name as a type argument. This example defines a like action that returns likes as an object:

// actions/index.ts
import { defineAction } from 'astro:actions';

export const server = {
  like: defineAction({
    handler: () => {
      /* ... */
      return { likes: 42 }
    }
  })
}

In your client code, you can infer this handler return value with ActionReturnType:

// client.ts
import { actions, ActionReturnType } from 'astro:actions';

type LikesResult = ActionReturnType<typeof actions.like>;
// -> { likes: number }