Skip to content

Commit 2dd74c8

Browse files
fix: improve fields type for generic components (#14974)
* fix: improves fields type for generic components * changeset * fix generated types * cleaner type for intellisense --------- Co-authored-by: Elliott Johnson <elliott.johnson@vercel.com>
1 parent 3fdb3ad commit 2dd74c8

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

.changeset/shaky-bobcats-dig.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: improves fields type for generic components

packages/kit/src/exports/public.d.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
PrerenderUnseenRoutesHandlerValue,
1616
PrerenderOption,
1717
RequestOptions,
18-
RouteSegment
18+
RouteSegment,
19+
IsAny
1920
} from '../types/private.js';
2021
import { BuildData, SSRNodeLoader, SSRRoute, ValidatedConfig } from 'types';
2122
import { SvelteConfig } from '@sveltejs/vite-plugin-svelte';
@@ -1953,6 +1954,18 @@ type UnknownField<Value> = RemoteFormFieldMethods<Value> & {
19531954
[key: string | number]: UnknownField<any>;
19541955
};
19551956

1957+
type RemoteFormFieldsRoot<Input extends RemoteFormInput | void> =
1958+
IsAny<Input> extends true
1959+
? RecursiveFormFields
1960+
: Input extends void
1961+
? {
1962+
/** Validation issues, if any */
1963+
issues(): RemoteFormIssue[] | undefined;
1964+
/** Validation issues belonging to this or any of the fields that belong to it, if any */
1965+
allIssues(): RemoteFormIssue[] | undefined;
1966+
}
1967+
: RemoteFormFields<Input>;
1968+
19561969
/**
19571970
* Recursive type to build form fields structure with proxy access
19581971
*/
@@ -2077,7 +2090,7 @@ export type RemoteForm<Input extends RemoteFormInput | void, Output> = {
20772090
/** The number of pending submissions */
20782091
get pending(): number;
20792092
/** Access form fields using object notation */
2080-
fields: RemoteFormFields<Input>;
2093+
fields: RemoteFormFieldsRoot<Input>;
20812094
};
20822095

20832096
/**

packages/kit/src/types/private.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,5 @@ export interface RouteSegment {
241241
}
242242

243243
export type TrailingSlash = 'never' | 'always' | 'ignore';
244+
245+
export type IsAny<T> = 0 extends 1 & T ? true : false;

packages/kit/test/types/remote.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { query, prerender, command, form } from '$app/server';
22
import { StandardSchemaV1 } from '@standard-schema/spec';
3-
import { RemotePrerenderFunction, RemoteQueryFunction, invalid } from '@sveltejs/kit';
3+
import {
4+
RemoteForm,
5+
RemoteFormInput,
6+
RemotePrerenderFunction,
7+
RemoteQueryFunction,
8+
invalid
9+
} from '@sveltejs/kit';
410

511
const schema: StandardSchemaV1<string> = null as any;
612
const schema2: StandardSchemaV1<string, number> = null as any;
@@ -359,6 +365,16 @@ function form_tests() {
359365
// doesn't use data
360366
const f9 = form(() => Promise.resolve({ success: true }));
361367
f9.result?.success === true;
368+
369+
// generic form
370+
function f10<
371+
Schema extends StandardSchemaV1<RemoteFormInput, unknown>,
372+
Form extends RemoteForm<StandardSchemaV1.InferInput<Schema>, unknown>
373+
>(data: StandardSchemaV1.InferInput<Schema>, form: Form) {
374+
form.fields.set(data);
375+
form.fields.allIssues();
376+
}
377+
void f10;
362378
}
363379
form_tests();
364380

packages/kit/types/index.d.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,18 @@ declare module '@sveltejs/kit' {
19291929
[key: string | number]: UnknownField<any>;
19301930
};
19311931

1932+
type RemoteFormFieldsRoot<Input extends RemoteFormInput | void> =
1933+
IsAny<Input> extends true
1934+
? RecursiveFormFields
1935+
: Input extends void
1936+
? {
1937+
/** Validation issues, if any */
1938+
issues(): RemoteFormIssue[] | undefined;
1939+
/** Validation issues belonging to this or any of the fields that belong to it, if any */
1940+
allIssues(): RemoteFormIssue[] | undefined;
1941+
}
1942+
: RemoteFormFields<Input>;
1943+
19321944
/**
19331945
* Recursive type to build form fields structure with proxy access
19341946
*/
@@ -2053,7 +2065,7 @@ declare module '@sveltejs/kit' {
20532065
/** The number of pending submissions */
20542066
get pending(): number;
20552067
/** Access form fields using object notation */
2056-
fields: RemoteFormFields<Input>;
2068+
fields: RemoteFormFieldsRoot<Input>;
20572069
};
20582070

20592071
/**
@@ -2371,6 +2383,8 @@ declare module '@sveltejs/kit' {
23712383
}
23722384

23732385
type TrailingSlash = 'never' | 'always' | 'ignore';
2386+
2387+
type IsAny<T> = 0 extends 1 & T ? true : false;
23742388
interface Asset {
23752389
file: string;
23762390
size: number;

0 commit comments

Comments
 (0)