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
- Pass the class constructor itself, not an instance: `register(Foo)` not `register(new Foo())`.
- Set your compile target to ES2015+ so class syntax survives transpilation.
- If importing, check ESM/CJS interop — you may need `mod.default`.
- 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
- Pass the constructor (MyClass), not an instance (new MyClass()) — the error message showing 'Object' is the tell
- Detection relies on toString() starting with 'class' — transpilers that downlevel classes to functions (old TS/Babel targets) break this; set target ES2015+
- Regular functions used as constructors won't pass — declare real class syntax
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
- Expected value which is `AsyncFunction`, received value of t
- Expected value which is `empty map`, received value of type
- Expected value which is `empty object`, received value of ty
- Expected value which is `empty set`, received value of type
- Expected value which is `empty string`, received value of ty
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/9bb5e79cbf9f83a1.json.
Report an issue: GitHub ↗.