From ce779a184884cd9f3793bbf249572b853fd49de4 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Fri, 3 Feb 2023 12:56:03 +0100 Subject: [PATCH 1/2] fix: ensure types of all form actions are accessible fixes #8865 --- .changeset/breezy-cycles-thank.md | 5 +++++ packages/kit/test/types/actions.test.ts | 12 ++++++++++++ packages/kit/types/index.d.ts | 8 +++++--- 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 .changeset/breezy-cycles-thank.md diff --git a/.changeset/breezy-cycles-thank.md b/.changeset/breezy-cycles-thank.md new file mode 100644 index 000000000000..23d8356a9bae --- /dev/null +++ b/.changeset/breezy-cycles-thank.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: ensure types of all form actions are accessible even if differing diff --git a/packages/kit/test/types/actions.test.ts b/packages/kit/test/types/actions.test.ts index 35a32f2011da..e7f7ccb5c25e 100644 --- a/packages/kit/test/types/actions.test.ts +++ b/packages/kit/test/types/actions.test.ts @@ -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 thas has all types accessible +type Actions2 = { + foo: () => Promise<{ message: string }>; + bar: () => Promise<{ success: boolean }>; +}; + +let form2: Kit.AwaitedActions = null as any; +form2.message = ''; +form2.success = true; +// @ts-expect-error - cannot both be present at the same time +form2 = { message: '', success: true }; diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index e7440202a024..ccc9a9d5e329 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -50,9 +50,11 @@ export type AwaitedProperties | void> = ? OptionalUnion> : AwaitedPropertiesUnion; -export type AwaitedActions any>> = { - [Key in keyof T]: OptionalUnion>>>; -}[keyof T]; +export type AwaitedActions any>> = OptionalUnion< + { + [Key in keyof T]: UnpackValidationError>>; + }[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 From 2f670650e85610734a2c23f24e7cc45801afce36 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Fri, 3 Feb 2023 12:57:02 +0100 Subject: [PATCH 2/2] THAS HAS --- packages/kit/test/types/actions.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/test/types/actions.test.ts b/packages/kit/test/types/actions.test.ts index e7f7ccb5c25e..8ca089d18c28 100644 --- a/packages/kit/test/types/actions.test.ts +++ b/packages/kit/test/types/actions.test.ts @@ -12,7 +12,7 @@ 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 thas has all types accessible +// 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 }>;