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

fix: improves fields type for generic components
17 changes: 15 additions & 2 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
PrerenderUnseenRoutesHandlerValue,
PrerenderOption,
RequestOptions,
RouteSegment
RouteSegment,
IsAny
} from '../types/private.js';
import { BuildData, SSRNodeLoader, SSRRoute, ValidatedConfig } from 'types';
import { SvelteConfig } from '@sveltejs/vite-plugin-svelte';
Expand Down Expand Up @@ -1949,6 +1950,18 @@ type UnknownField<Value> = RemoteFormFieldMethods<Value> & {
[key: string | number]: UnknownField<any>;
};

type RemoteFormFieldsRoot<Input extends RemoteFormInput | void> =
IsAny<Input> extends true
? RecursiveFormFields
: Input extends void
? {
/** Validation issues, if any */
issues(): RemoteFormIssue[] | undefined;
/** Validation issues belonging to this or any of the fields that belong to it, if any */
allIssues(): RemoteFormIssue[] | undefined;
}
: RemoteFormFields<Input>;

/**
* Recursive type to build form fields structure with proxy access
*/
Expand Down Expand Up @@ -2073,7 +2086,7 @@ export type RemoteForm<Input extends RemoteFormInput | void, Output> = {
/** The number of pending submissions */
get pending(): number;
/** Access form fields using object notation */
fields: RemoteFormFields<Input>;
fields: RemoteFormFieldsRoot<Input>;
/** Spread this onto a `<button>` or `<input type="submit">` */
buttonProps: {
type: 'submit';
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/types/private.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,5 @@ export interface RouteSegment {
}

export type TrailingSlash = 'never' | 'always' | 'ignore';

export type IsAny<T> = 0 extends 1 & T ? true : false;
18 changes: 17 additions & 1 deletion packages/kit/test/types/remote.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { query, prerender, command, form } from '$app/server';
import { StandardSchemaV1 } from '@standard-schema/spec';
import { RemotePrerenderFunction, RemoteQueryFunction, invalid } from '@sveltejs/kit';
import {
RemoteForm,
RemoteFormInput,
RemotePrerenderFunction,
RemoteQueryFunction,
invalid
} from '@sveltejs/kit';

const schema: StandardSchemaV1<string> = null as any;
const schema2: StandardSchemaV1<string, number> = null as any;
Expand Down Expand Up @@ -359,6 +365,16 @@ function form_tests() {
// doesn't use data
const f9 = form(() => Promise.resolve({ success: true }));
f9.result?.success === true;

// generic form
function f10<
Schema extends StandardSchemaV1<RemoteFormInput, unknown>,
Form extends RemoteForm<StandardSchemaV1.InferInput<Schema>, unknown>
>(data: StandardSchemaV1.InferInput<Schema>, form: Form) {
form.fields.set(data);
form.fields.allIssues();
}
void f10;
}
form_tests();

Expand Down
16 changes: 15 additions & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,18 @@ declare module '@sveltejs/kit' {
[key: string | number]: UnknownField<any>;
};

type RemoteFormFieldsRoot<Input extends RemoteFormInput | void> =
IsAny<Input> extends true
? RecursiveFormFields
: Input extends void
? {
/** Validation issues, if any */
issues(): RemoteFormIssue[] | undefined;
/** Validation issues belonging to this or any of the fields that belong to it, if any */
allIssues(): RemoteFormIssue[] | undefined;
}
: RemoteFormFields<Input>;

/**
* Recursive type to build form fields structure with proxy access
*/
Expand Down Expand Up @@ -2049,7 +2061,7 @@ declare module '@sveltejs/kit' {
/** The number of pending submissions */
get pending(): number;
/** Access form fields using object notation */
fields: RemoteFormFields<Input>;
fields: RemoteFormFieldsRoot<Input>;
/** Spread this onto a `<button>` or `<input type="submit">` */
buttonProps: {
type: 'submit';
Expand Down Expand Up @@ -2391,6 +2403,8 @@ declare module '@sveltejs/kit' {
}

type TrailingSlash = 'never' | 'always' | 'ignore';

type IsAny<T> = 0 extends 1 & T ? true : false;
interface Asset {
file: string;
size: number;
Expand Down
Loading