sindresorhus/is · error · TypeError

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

Error message

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

What it means

Thrown by `assertGenerator()` when `isGenerator()` fails — the value is not a generator *object* (the iterator returned by invoking a generator function, tagged `Generator`). Note the distinction: the generator function itself does not pass this check.

Source

Thrown at source/index.ts:1647

	}
}

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)) {
		throw new TypeError(message ?? typeErrorMessage('GeneratorFunction', value));
	}
}

export function assertHtmlElement(value: unknown, message?: string): asserts value is HTMLElement {
	if (!isHtmlElement(value)) {
		throw new TypeError(message ?? typeErrorMessage('HTMLElement', value));
	}
}

export function assertInfinite(value: unknown, message?: string): asserts value is number {
	if (!isInfinite(value)) {
		throw new TypeError(message ?? typeErrorMessage('infinite number', value));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Invoke the generator function first and assert the returned object: `assertGenerator(gen())`.
  2. If the value is an async generator, use `is.asyncGenerator` / `assertAsyncGenerator` instead.
  3. If any iterable is acceptable, assert with `is.iterable` rather than `is.generator`.

Example fix

// before
assertGenerator(numbersGen); // the function itself
// after
assertGenerator(numbersGen()); // the generator object
Defensive patterns

Strategy: type-guard

Validate before calling

const isGen = value != null && typeof value.next === 'function' && typeof value.throw === 'function' && typeof value[Symbol.iterator] === 'function';
if (!isGen) throw new TypeError('Expected Generator object');

Type guard

function isGenerator(value: unknown): value is Generator {
  const v = value as Generator | null;
  return v != null && typeof v.next === 'function' && typeof v.throw === 'function' && typeof v.return === 'function';
}

Try / catch

try {
  assert.generator(value);
} catch (error) {
  if (error instanceof TypeError) { /* likely passed the generator function, not its result */ }
  else throw error;
}

Prevention

When it happens

Trigger: `is.assert.generator(value)` called with a generator function that was never invoked, a plain iterator/iterable (arrays, custom `[Symbol.iterator]` objects), or an async generator object.

Common situations: Passing `myGenFn` instead of `myGenFn()`; assuming any iterable qualifies — plain iterators lack the `Generator` tag and `throw`/`return` semantics; mixing up sync and async generators (async ones need `isAsyncGenerator`).

Related errors


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