modular-forms
modular-forms copied to clipboard
fix accessing array field type by path
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
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?
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?
You can leave it as it is. I think that's fine.