Skip to content

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

Open
@fzembow

Description

@fzembow
  • Before posting an issue, read the FAQ and search the previous issues.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requestedtypesSomething related to types

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions