Skip to content

Commit 64f484f

Browse files
sillvvateemingc
andauthored
fix: deep partial .value() and .set(...) types for forms (#14837)
fixes #14835 The form's default values are an empty object `{}` when defaults are not provided by `.set()`. Therefore, the `.value()` getter should return a deep partial of the schema type as all values are initially undefined. Plus, deep partial `.set()` allows setting _some_, but not _all_ values initially. [`DeepPartial` Demo](https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true#code/C4TwDgpgBAIhFgAoEMBOwCWyA2AeAKgHxQC8AsAFACQ+UEAHsBAHYAmAzlAEoQDGA9qla5EqfpHQgA0hBAAaKAFdmAa2b8A7s2IAfJavVaA2gF1KVKgH4oAb3MWqRqVAzMoK2fwBmUfCcsAXL5OJnSMLBzcfILCouIQkjLy+mqa2lB6yqnGodZwCCjoWHj4IcRBpVImANz2VAC+9hUZ+qwQXq4QrLUUlALM7MBQTINB+UhomDi4dhRQ81D8AEYAVkE2wxCjsMhMUPVylAtQaKjrm9uDqK4A5vumh3MLqHxBPAJCuFe3Ct-MN4RKPViCRbJQgeCKCNgAA6ZYrSww6GUAD0KIWAD1LJRoTDToijAAGfxIrbAVHo+ZYnFkmEvXiI5EUNGY7G9Cj9QYXYAAJjG8AmRWmfwBpCgACJxRTjligA) --- ### Please don't delete this checklist! Before submitting the PR, please make sure you do the following: - [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs - [x] This message body should clearly illustrate what problems it solves. - [ ] Ideally, include a test that fails without this PR but passes with it. ### Tests - [x] Run the tests with `pnpm test` and lint the project with `pnpm lint` and `pnpm check` ### Changesets - [x] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running `pnpm changeset` and following the prompts. Changesets that add features should be `minor` and those that fix bugs should be `patch`. Please prefix changeset messages with `feat:`, `fix:`, or `chore:`. ### Edits - [x] Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed. --------- Co-authored-by: Tee Ming <[email protected]>
1 parent d28d372 commit 64f484f

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

.changeset/nice-chairs-switch.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: use deep partial types for form remote functions `.value()` and `.set(...)`

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
PrerenderOption,
1717
RequestOptions,
1818
RouteSegment,
19+
DeepPartial,
1920
IsAny
2021
} from '../types/private.js';
2122
import { BuildData, SSRNodeLoader, SSRRoute, ValidatedConfig } from 'types';
@@ -1910,9 +1911,9 @@ type InputElementProps<T extends keyof InputTypeMap> = T extends 'checkbox' | 'r
19101911

19111912
type RemoteFormFieldMethods<T> = {
19121913
/** The values that will be submitted */
1913-
value(): T;
1914+
value(): DeepPartial<T>;
19141915
/** Set the values that will be submitted */
1915-
set(input: T): T;
1916+
set(input: DeepPartial<T>): DeepPartial<T>;
19161917
/** Validation issues, if any */
19171918
issues(): RemoteFormIssue[] | undefined;
19181919
};

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,12 @@ export interface RouteSegment {
243243
/** @default 'never' */
244244
export type TrailingSlash = 'never' | 'always' | 'ignore';
245245

246+
export type DeepPartial<T> = T extends Record<PropertyKey, unknown> | unknown[]
247+
? {
248+
[K in keyof T]?: T[K] extends Record<PropertyKey, unknown> | unknown[]
249+
? DeepPartial<T[K]>
250+
: T[K];
251+
}
252+
: T | undefined;
253+
246254
export type IsAny<T> = 0 extends 1 & T ? true : false;

packages/kit/types/index.d.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,9 +1885,9 @@ declare module '@sveltejs/kit' {
18851885

18861886
type RemoteFormFieldMethods<T> = {
18871887
/** The values that will be submitted */
1888-
value(): T;
1888+
value(): DeepPartial<T>;
18891889
/** Set the values that will be submitted */
1890-
set(input: T): T;
1890+
set(input: DeepPartial<T>): DeepPartial<T>;
18911891
/** Validation issues, if any */
18921892
issues(): RemoteFormIssue[] | undefined;
18931893
};
@@ -2402,6 +2402,14 @@ declare module '@sveltejs/kit' {
24022402
/** @default 'never' */
24032403
type TrailingSlash = 'never' | 'always' | 'ignore';
24042404

2405+
type DeepPartial<T> = T extends Record<PropertyKey, unknown> | unknown[]
2406+
? {
2407+
[K in keyof T]?: T[K] extends Record<PropertyKey, unknown> | unknown[]
2408+
? DeepPartial<T[K]>
2409+
: T[K];
2410+
}
2411+
: T | undefined;
2412+
24052413
type IsAny<T> = 0 extends 1 & T ? true : false;
24062414
interface Asset {
24072415
file: string;

0 commit comments

Comments
 (0)