diff options
-rw-r--r-- | .changeset/gold-seas-crash.md | 5 | ||||
-rw-r--r-- | packages/astro/src/actions/runtime/virtual/server.ts | 2 | ||||
-rw-r--r-- | packages/astro/test/types/action-return-type.ts | 19 |
3 files changed, 21 insertions, 5 deletions
diff --git a/.changeset/gold-seas-crash.md b/.changeset/gold-seas-crash.md new file mode 100644 index 000000000..86904fca2 --- /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 a711f1032..7aea22b2f 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<TInputSchema>, ) => Promise<Awaited<TOutput>>; } - : (input?: any) => Promise<SafeResult<never, Awaited<TOutput>>> & { + : ((input?: any) => Promise<SafeResult<never, Awaited<TOutput>>>) & { orThrow: (input?: any) => Promise<Awaited<TOutput>>; }; diff --git a/packages/astro/test/types/action-return-type.ts b/packages/astro/test/types/action-return-type.ts index 05add9cb3..d8a1b7231 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<ActionReturnType<typeof action>>().toEqualTypeOf< + expectTypeOf<ActionReturnType<typeof _action>>().toEqualTypeOf< SafeResult<any, { name: string }> >(); - expectTypeOf<ActionReturnType<typeof action.orThrow>>().toEqualTypeOf<{ name: string }>(); + expectTypeOf<ActionReturnType<typeof _action.orThrow>>().toEqualTypeOf<{ name: string }>(); + }); + + it('Infers action return type when input is omitted', async () => { + const _action = defineAction({ + handler: async () => { + return { name: 'Ben' }; + }, + }); + expectTypeOf<ActionReturnType<typeof _action>>().toEqualTypeOf< + SafeResult<any, { name: string }> + >(); + expectTypeOf<ActionReturnType<typeof _action.orThrow>>().toEqualTypeOf<{ name: string }>(); }); }); |