ciscoheat / sveltekit-superforms

Making SvelteKit forms a pleasure to use!
https://superforms.rocks
MIT License
2.13k stars 63 forks source link

Type instantiation is excessively deep and possibly infinite with componentized fields #463

Open fzembow opened 1 month ago

fzembow commented 1 month ago

Description

I'm trying to even further componentize the approach in the componentization guide, by allowing users to pass an array of the FormPathLeaves to render the form fields.

However, I'm seeing a TS error, "Type instantiation is excessively deep and possibly infinite" when trying to use FormPathLeaves as a variable, rather than hardcoding it. You can see three parallel approaches in the below snippet.

<script lang="ts">
  import { z } from 'zod';
  import type {
    SuperValidated,
    Infer,
    FormPathLeaves,
  } from 'sveltekit-superforms';
  import { superForm } from 'sveltekit-superforms';
  import TextField from './TextField.svelte';

  export const userFormSchema = z.object({
    name: z.string().default('Hello world!'),
    email: z.string().email(),
  });

  type UserFormSchema = typeof userFormSchema;

  export let data: SuperValidated<Infer<UserFormSchema>>;
  const pathLeaves: FormPathLeaves<Infer<UserFormSchema>>[] = ['name', 'email'];

  const constPathLeaves = ['name', 'email'] as const;

  const form = superForm(data);
  const { enhance } = form;
</script>

<form method="POST" use:enhance>
  {#each pathLeaves as field}
    <!-- TS error: Type instantiation is excessively deep and possibly infinite. -->
    <TextField superform={form} {field} />
  {/each}

  <!-- Directly specifying the values is fine. -->
  <TextField superform={form} field="name" />
  <TextField superform={form} field="email" />

  {#each constPathLeaves as field}
    <!-- Using "as const" is also fine. -->
    <TextField superform={form} {field} />
  {/each}

  <div><button>Submit</button></div>
</form>

This may very well be some limitation of my typescript knowledge... but is this just not intrinsically possible? Should I be listing out the paths with a different type annotation?

If applicable, a MRE

See this example, which has the error on Sveltelab when you view the LoginForm.svelte file: EXAMPLE

ciscoheat commented 1 month ago

I can't see any LoginForm.svelte in the example link?

fzembow commented 1 month ago

@ciscoheat I'm sorry - I guess sveltelab didn't save my changes earlier :(

Here is a corrected repro link: https://www.sveltelab.dev/fh9ym08604bfrxv

If you click into LoginForm.svelte now, the issue will appear

image

Again, this might be a limitation of Typescript (or my knowledge thereof) but I am very thankful that you would take a look -- to me, extending the componentization even further was the natural thing to do.

ciscoheat commented 1 month ago

I see now, I should have looked closer at the code example. You need const, otherwise it will treat each value as string and that will probably mess up the typing. The types behind the scenes are quite complicated, especially with generics.

I did manage to remove the error once locally, so try updating to latest typescript (and maybe latest svelte 5) and maybe it will work.