Skip to content

Commit

Permalink
fix: ensure types of all form actions are accessible (#8877)
Browse files Browse the repository at this point in the history
* fix: ensure types of all form actions are accessible

fixes #8865

* THAS HAS
  • Loading branch information
dummdidumm authored Feb 3, 2023
1 parent 2ef9b7d commit f8dd775
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-cycles-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: ensure types of all form actions are accessible even if differing
12 changes: 12 additions & 0 deletions packages/kit/test/types/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ form.message = '';
form.success = true;
// @ts-expect-error - cannot both be present at the same time
form = { message: '', success: true };

// Test: Actions with different return types are transformed into a union that has all types accessible
type Actions2 = {
foo: () => Promise<{ message: string }>;
bar: () => Promise<{ success: boolean }>;
};

let form2: Kit.AwaitedActions<Actions2> = null as any;
form2.message = '';
form2.success = true;
// @ts-expect-error - cannot both be present at the same time
form2 = { message: '', success: true };
8 changes: 5 additions & 3 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ export type AwaitedProperties<input extends Record<string, any> | void> =
? OptionalUnion<AwaitedPropertiesUnion<input>>
: AwaitedPropertiesUnion<input>;

export type AwaitedActions<T extends Record<string, (...args: any) => any>> = {
[Key in keyof T]: OptionalUnion<UnpackValidationError<Awaited<ReturnType<T[Key]>>>>;
}[keyof T];
export type AwaitedActions<T extends Record<string, (...args: any) => any>> = OptionalUnion<
{
[Key in keyof T]: UnpackValidationError<Awaited<ReturnType<T[Key]>>>;
}[keyof T]
>;

// Takes a union type and returns a union type where each type also has all properties
// of all possible types (typed as undefined), making accessing them more ergonomic
Expand Down

0 comments on commit f8dd775

Please sign in to comment.