Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure types of all form actions are accessible #8877

Merged
merged 2 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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