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
- Invoke the generator function first and assert the returned object: `assertGenerator(gen())`.
- If the value is an async generator, use `is.asyncGenerator` / `assertAsyncGenerator` instead.
- 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
- Call the generator function first — pass `gen()` (a Generator object), not `gen` (a GeneratorFunction)
- A plain iterator or array is not a Generator; it must have next/return/throw with the generator prototype
- Don't confuse sync Generator with AsyncGenerator — `async function*` produces a different type
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
- Expected value which is `GeneratorFunction`, received value
- Expected value which is `Array`, received value of type `${i
- Expected value which is `array-like`, received value of type
- Expected value which is `BigUint64Array`, received value of
- Expected value which is `empty map`, received value of type
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/56fbd5d6e71001d7.json.
Report an issue: GitHub ↗.