Skip to content

formDataToObject

function formDataToObject<T>(formData): T;

Defined in: form-data-to-object-utils.ts:24

Convert a FormData into a plain object. A key that appears once becomes a single value; a key that appears more than once (checkbox groups, multi-selects) becomes an array, in submission order.

The type parameter is only a cast: nothing is validated or coerced, and values stay string (or File). A field that can repeat comes back as a single string when only one value was sent, so type it as string | string[] or run the result through a schema.

Type Parameter Default type
T Record<string, unknown>
Parameter Type
formData FormData

T

import { formDataToObject } from "@xtandard/lib/web";
const form = new FormData();
form.append("name", "Ada");
form.append("tag", "math");
form.append("tag", "code");
formDataToObject<{ name: string; tag: string | string[] }>(form);
// { name: "Ada", tag: ["math", "code"] }