From 13b912a8702afb96e2d0bc20dcc1b4135ae58147 Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Thu, 8 Aug 2024 11:53:33 -0400 Subject: [PATCH] Actions: fix missing orThrow type when input is omitted (#11658) * fix: orThrow missing when input is omitted * chore: changeset --- .changeset/gold-seas-crash.md | 5 +++++ .../src/actions/runtime/virtual/server.ts | 2 +- .../astro/test/types/action-return-type.ts | 19 +++++++++++++++---- 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 .changeset/gold-seas-crash.md diff --git a/.changeset/gold-seas-crash.md b/.changeset/gold-seas-crash.md new file mode 100644 index 000000000000..86904fca2f2a --- /dev/null +++ b/.changeset/gold-seas-crash.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes `orThrow()` type when calling an Action without an `input` validator. diff --git a/packages/astro/src/actions/runtime/virtual/server.ts b/packages/astro/src/actions/runtime/virtual/server.ts index a711f10326fc..7aea22b2fc7f 100644 --- a/packages/astro/src/actions/runtime/virtual/server.ts +++ b/packages/astro/src/actions/runtime/virtual/server.ts @@ -39,7 +39,7 @@ export type ActionClient< input: TAccept extends 'form' ? FormData : z.input, ) => Promise>; } - : (input?: any) => Promise>> & { + : ((input?: any) => Promise>>) & { orThrow: (input?: any) => Promise>; }; diff --git a/packages/astro/test/types/action-return-type.ts b/packages/astro/test/types/action-return-type.ts index 05add9cb3f96..d8a1b72318ef 100644 --- a/packages/astro/test/types/action-return-type.ts +++ b/packages/astro/test/types/action-return-type.ts @@ -9,8 +9,7 @@ import { z } from '../../zod.mjs'; describe('ActionReturnType', () => { it('Infers action return type', async () => { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const action = defineAction({ + const _action = defineAction({ input: z.object({ name: z.string(), }), @@ -18,9 +17,21 @@ describe('ActionReturnType', () => { return { name }; }, }); - expectTypeOf>().toEqualTypeOf< + expectTypeOf>().toEqualTypeOf< SafeResult >(); - expectTypeOf>().toEqualTypeOf<{ name: string }>(); + expectTypeOf>().toEqualTypeOf<{ name: string }>(); + }); + + it('Infers action return type when input is omitted', async () => { + const _action = defineAction({ + handler: async () => { + return { name: 'Ben' }; + }, + }); + expectTypeOf>().toEqualTypeOf< + SafeResult + >(); + expectTypeOf>().toEqualTypeOf<{ name: string }>(); }); });