Skip to content
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/upset-hotels-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: `RecursiveFormFields` type for recursive or unknown schemas
32 changes: 28 additions & 4 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1914,22 +1914,46 @@ type RemoteFormFieldContainer<Value> = RemoteFormFieldMethods<Value> & {
allIssues(): RemoteFormIssue[] | undefined;
};

type UnknownField<Value> = RemoteFormFieldMethods<Value> & {
/** Validation issues belonging to this or any of the fields that belong to it, if any */
allIssues(): RemoteFormIssue[] | undefined;
/**
* Returns an object that can be spread onto an input element with the correct type attribute,
* aria-invalid attribute if the field is invalid, and appropriate value/checked property getters/setters.
* @example
* ```svelte
* <input {...myForm.fields.myString.as('text')} />
* <input {...myForm.fields.myNumber.as('number')} />
* <input {...myForm.fields.myBoolean.as('checkbox')} />
* ```
*/
as<T extends RemoteFormFieldType<Value>>(...args: AsArgs<T, Value>): InputElementProps<T>;
} & {
[key: string | number]: UnknownField<any>;
};

/**
* Recursive type to build form fields structure with proxy access
*/
type RemoteFormFields<T> =
export type RemoteFormFields<T> =
WillRecurseIndefinitely<T> extends true
? RecursiveFormFields
: NonNullable<T> extends string | number | boolean | File
? RemoteFormField<NonNullable<T>>
: T extends string[] | File[]
? RemoteFormField<T> & { [K in number]: RemoteFormField<T[number]> }
: T extends Array<infer U>
? RemoteFormFieldContainer<T> & { [K in number]: RemoteFormFields<U> }
: RemoteFormFieldContainer<T> & { [K in keyof T]-?: RemoteFormFields<T[K]> };
? RemoteFormFieldContainer<T> & {
[K in number]: RemoteFormFields<U>;
}
: RemoteFormFieldContainer<T> & {
[K in keyof T]-?: RemoteFormFields<T[K]>;
};

// By breaking this out into its own type, we avoid the TS recursion depth limit
type RecursiveFormFields = RemoteFormField<any> & { [key: string | number]: RecursiveFormFields };
type RecursiveFormFields = RemoteFormFieldContainer<any> & {
[key: string | number]: UnknownField<any>;
};

type MaybeArray<T> = T | T[];

Expand Down
32 changes: 28 additions & 4 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1890,22 +1890,46 @@ declare module '@sveltejs/kit' {
allIssues(): RemoteFormIssue[] | undefined;
};

type UnknownField<Value> = RemoteFormFieldMethods<Value> & {
/** Validation issues belonging to this or any of the fields that belong to it, if any */
allIssues(): RemoteFormIssue[] | undefined;
/**
* Returns an object that can be spread onto an input element with the correct type attribute,
* aria-invalid attribute if the field is invalid, and appropriate value/checked property getters/setters.
* @example
* ```svelte
* <input {...myForm.fields.myString.as('text')} />
* <input {...myForm.fields.myNumber.as('number')} />
* <input {...myForm.fields.myBoolean.as('checkbox')} />
* ```
*/
as<T extends RemoteFormFieldType<Value>>(...args: AsArgs<T, Value>): InputElementProps<T>;
} & {
[key: string | number]: UnknownField<any>;
};

/**
* Recursive type to build form fields structure with proxy access
*/
type RemoteFormFields<T> =
export type RemoteFormFields<T> =
WillRecurseIndefinitely<T> extends true
? RecursiveFormFields
: NonNullable<T> extends string | number | boolean | File
? RemoteFormField<NonNullable<T>>
: T extends string[] | File[]
? RemoteFormField<T> & { [K in number]: RemoteFormField<T[number]> }
: T extends Array<infer U>
? RemoteFormFieldContainer<T> & { [K in number]: RemoteFormFields<U> }
: RemoteFormFieldContainer<T> & { [K in keyof T]-?: RemoteFormFields<T[K]> };
? RemoteFormFieldContainer<T> & {
[K in number]: RemoteFormFields<U>;
}
: RemoteFormFieldContainer<T> & {
[K in keyof T]-?: RemoteFormFields<T[K]>;
};

// By breaking this out into its own type, we avoid the TS recursion depth limit
type RecursiveFormFields = RemoteFormField<any> & { [key: string | number]: RecursiveFormFields };
type RecursiveFormFields = RemoteFormFieldContainer<any> & {
[key: string | number]: UnknownField<any>;
};

type MaybeArray<T> = T | T[];

Expand Down
Loading