sindresorhus/is · error · TypeError

Expected value which is `Class`, received value of type `${i

Error message

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

What it means

Thrown by `assertClass()` when the value fails `isClass` — a function whose string form starts with `class` (native class syntax). It guarantees the value can be used with `new` as a proper constructor of type `Class<T>`.

Source

Thrown at source/index.ts:1532

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

/**
Note: [Prefer using `Uint8Array` instead of `Buffer`.](https://sindresorhus.com/blog/goodbye-nodejs-buffer)
*/
export function assertBuffer(value: unknown, message?: string): asserts value is NodeBuffer {
	if (!isBuffer(value)) {
		throw new TypeError(message ?? typeErrorMessage('Buffer', value));
	}
}

export function assertClass<T>(value: unknown, message?: string): asserts value is Class<T> {
	if (!isClass(value)) {
		throw new TypeError(message ?? typeErrorMessage('Class', value));
	}
}

export function assertDataView(value: unknown, message?: string): asserts value is DataView {
	if (!isDataView(value)) {
		throw new TypeError(message ?? typeErrorMessage('DataView', value));
	}
}

export function assertDate(value: unknown, message?: string): asserts value is Date {
	if (!isDate(value)) {
		throw new TypeError(message ?? typeErrorMessage('Date', value));
	}
}

export function assertDirectInstanceOf<T>(instance: unknown, class_: Class<T>, message?: string): asserts instance is T {
	if (!isDirectInstanceOf(instance, class_)) {
		throw new TypeError(message ?? typeErrorMessage('T', instance));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Pass the class constructor itself, not an instance: `register(Foo)` not `register(new Foo())`.
  2. Set your compile target to ES2015+ so class syntax survives transpilation.
  3. If importing, check ESM/CJS interop — you may need `mod.default`.
  4. If the API legitimately accepts factory functions, use `assert.function_` instead.

Example fix

// before (tsconfig)
{"compilerOptions": {"target": "es5"}}
// after
{"compilerOptions": {"target": "es2020"}}
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof value !== 'function' || !/^\s*class\s/.test(Function.prototype.toString.call(value))) {
  // not a class constructor — pass the class itself, not an instance or factory
}

Type guard

function isClass(value: unknown): value is new (...args: any[]) => unknown {
  return typeof value === 'function' && /^\s*class[\s{]/.test(Function.prototype.toString.call(value));
}

Try / catch

try {
  assert.class_(value);
} catch (error) {
  if (error instanceof TypeError) {
    // an instance or plain function was passed where a class was expected — pass MyClass, not new MyClass()
  } else throw error;
}

Prevention

When it happens

Trigger: Calling `assert.class_(value)` with an instance instead of the class itself, a plain/arrow function, an ES5 constructor-function 'class', or a class transpiled down to a function by an old compilation target.

Common situations: Passing `new Foo()` where `Foo` was expected (or vice versa); TypeScript/Babel targeting ES5 turns `class` into `function`, so `isClass` fails at runtime even though the source uses class syntax; DI/plugin registries receiving factory functions instead of classes; default-export interop (`module.default` vs module) yielding the wrong value.

Related errors


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