sindresorhus/is · error · TypeError

Expected value which is `FormData`, received value of type `

Error message

Expected value which is `FormData`, received value of type `${is(value)}`.

What it means

Thrown by `assertFormData()` when `isFormData()` fails — the value is not an instance of the platform `FormData` class. It guarantees downstream code can safely call FormData methods like `append` and `entries`.

Source

Thrown at source/index.ts:1634

		throw new TypeError(message ?? typeErrorMessage('finite number', value));
	}
}

export function assertFloat32Array(value: unknown, message?: string): asserts value is Float32Array {
	if (!isFloat32Array(value)) {
		throw new TypeError(message ?? typeErrorMessage('Float32Array', value));
	}
}

export function assertFloat64Array(value: unknown, message?: string): asserts value is Float64Array {
	if (!isFloat64Array(value)) {
		throw new TypeError(message ?? typeErrorMessage('Float64Array', value));
	}
}

export function assertFormData(value: unknown, message?: string): asserts value is FormData {
	if (!isFormData(value)) {
		throw new TypeError(message ?? typeErrorMessage('FormData', value));
	}
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export function assertFunction(value: unknown, message?: string): asserts value is Function {
	if (!isFunction(value)) {
		throw new TypeError(message ?? typeErrorMessage('Function', value));
	}
}

export function assertGenerator(value: unknown, message?: string): asserts value is Generator {
	if (!isGenerator(value)) {
		throw new TypeError(message ?? typeErrorMessage('Generator', value));
	}
}

export function assertGeneratorFunction(value: unknown, message?: string): asserts value is GeneratorFunction {
	if (!isGeneratorFunction(value)) {

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Construct a real FormData: `const fd = new FormData(); fd.append(...)` or `new FormData(formElement)`.
  2. On Node, ensure Node ≥18 (global WHATWG FormData) or import it from `undici` instead of the legacy `form-data` package.
  3. If the data is a plain object by design, validate with `is.plainObject` and convert to FormData at the network boundary.

Example fix

// before
const body = {name: 'x', file};
assertFormData(body);
// after
const body = new FormData();
body.append('name', 'x');
body.append('file', file);
assertFormData(body);
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof FormData === 'undefined' || !(value instanceof FormData)) {
  throw new TypeError('Expected FormData');
}

Type guard

function isFormData(value: unknown): value is FormData {
  return typeof FormData !== 'undefined' && value instanceof FormData;
}

Try / catch

try {
  assert.formData(value);
} catch (error) {
  if (error instanceof TypeError) { /* build a FormData from the plain object instead */ }
  else throw error;
}

Prevention

When it happens

Trigger: `is.assert.formData(value)` with a plain object of key/value pairs, a URLSearchParams instance, a serialized string, or a polyfilled/third-party form-data object (e.g. the npm `form-data` package in Node) that isn't the global `FormData`.

Common situations: Node.js versions before 18 lacking a global FormData; using the `form-data` npm package with fetch wrappers while validation expects the WHATWG FormData; passing `Object.fromEntries(form)` output or raw request bodies instead of the FormData object itself.

Related errors


AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31). Data as JSON: /data/errors/93865cfe5eba1f40.json. Report an issue: GitHub ↗.