Skip to content

Commit

Permalink
[fix] #6823 - some properties in type of object returned from ActionD…
Browse files Browse the repository at this point in the history
…ata is missing (#6869)

Fixes #6823
Fixes #6822

Co-authored-by: William Viktorsson <wilvik05@edu.umea.se>
Co-authored-by: williamv <williamv@cs.umu.se>
  • Loading branch information
3 people authored Sep 19, 2022
1 parent 227d4c7 commit 33722bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-pots-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix to ActionData type generation
17 changes: 14 additions & 3 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,20 @@ export type AwaitedProperties<input extends Record<string, any> | void> = input
? input
: unknown;

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

// Makes sure a type is "repackaged" and therefore more readable
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
// 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
type OptionalUnion<
U extends Record<string, any>, // not unknown, else interfaces don't satisfy this constraint
A extends keyof U = U extends U ? keyof U : never
> = U extends unknown ? { [P in Exclude<A, keyof U>]?: never } & U : never;

// Needs to be here, else ActionData will be resolved to unknown - probably because of "d.ts file imports .js file" in combination with allowJs
interface ValidationError<T extends Record<string, unknown> | undefined = undefined> {
Expand Down

0 comments on commit 33722bd

Please sign in to comment.