0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-17 22:44:24 -05:00

Actions: fix missing orThrow type when input is omitted (#11658)

* fix: orThrow missing when input is omitted

* chore: changeset
This commit is contained in:
Ben Holmes 2024-08-08 11:53:33 -04:00 committed by GitHub
parent a851021498
commit 13b912a870
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 21 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes `orThrow()` type when calling an Action without an `input` validator.

View file

@ -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>>;
};

View file

@ -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 }>();
});
});