Skip to content

Remote forms: incorrect typing for form.fields.value() and form.fields.<fieldname>.value() #14835

@siim-m

Description

@siim-m

Describe the bug

The return values of form.fields.value() and form.fields.<fieldname>.value() are typed according to the schema used when defining the remote function.

However, this does not reflect runtime reality. The return value of form.fields.value() is an empty object until the user either interacts with the associated form input or values are set programmatically using form.fields.set(). Likewise, form.fields.<filedname>.value() returns undefined initially.

Reproduction

Full working reproduction: https://github.com/siim-m/sk-remote-form-type-issue

// src/lib/forms.remote.ts
import { form } from '$app/server';
import * as z from 'zod';

export const myForm = form(
  z.object({
    someString: z.string(),
  }),
  async (input) => {
    console.log('input', input);
  }
);
<script lang="ts">
  // src/routes/+page.svelte
  import { myForm } from '$lib/forms.remote';

  // Until the user enters anything in the input, TypeScript types don't reflect runtime reality:
  // - `values` is typed as `{ someString: string }` but is an empty object
  // - `someString` is typed as `string` but is `undefined` at runtime

  const values = $derived(myForm.fields.value());

  const someString = $derived(myForm.fields.someString.value());
</script>

<form {...myForm}>
  <input {...myForm.fields.someString.as('text')} />
</form>

<p>values:</p>
<pre>{JSON.stringify(values, null, 2)}</pre>
<hr />
<p>typeof someString:</p>
<pre>{typeof someString}</pre>
Image

Logs

(nothing printed to console)

System Info

System:
    OS: Linux 6.8 Ubuntu 24.04.3 LTS 24.04.3 LTS (Noble Numbat)
    CPU: (8) x64 QEMU Virtual CPU version 2.5+
    Memory: 42.96 GB / 62.66 GB
    Container: Yes
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.19.0 - /home/siim/.config/nvm/versions/node/v22.19.0/bin/node
    npm: 11.6.0 - /home/siim/.config/nvm/versions/node/v22.19.0/bin/npm
    bun: 1.3.0 - /home/siim/.bun/bin/bun
  npmPackages:
    @sveltejs/adapter-auto: ^6.1.0 => 6.1.1
    @sveltejs/kit: 2.48.2 => 2.48.2
    @sveltejs/vite-plugin-svelte: ^6.2.0 => 6.2.1
    svelte: 5.40.2 => 5.40.2
    vite: ^7.1.7 => 7.1.12

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions