modular-forms icon indicating copy to clipboard operation
modular-forms copied to clipboard

fix accessing array field type by path

Open create-signal opened this issue 6 months ago • 3 comments

The Field component seems to show type errors when used with an Array field that is not an object - for example:

const TestComponent = () => {
  const [form, { Form, Field }] = createForm<{ values: number[] }>({
    initialValues: { values: [1, 2, 3] },
  });

  return (
    <Form onSubmit={() => {}}>
      <Field name="values.0" type="number">
        {(field, props) => (
          <input type="number" {...props} value={field.value} />
        )}
      </Field>
    </Form>
  );
};

Will show the errors

  • on the "type" prop - "Type 'string' is not assignable to type 'undefined'."
  • the type of "field.value" will be "undefined"

This PR updates the PathValue helper to allow looking up a value by array index

create-signal avatar May 27 '25 05:05 create-signal

Hey! This library is currently kind of in maintenance mode as I plan to rewrite it at some point. Is this issue preventing you from using the library?

fabian-hiller avatar May 29 '25 01:05 fabian-hiller

Nope! The type errors can be suppressed with @ts-expect-error and the library works as expected. Perhaps I turn this PR into an Issue instead to make it more easily searchable for others experiencing the same issue?

create-signal avatar May 29 '25 05:05 create-signal

You can leave it as it is. I think that's fine.

fabian-hiller avatar Jun 11 '25 02:06 fabian-hiller