babel/babel · error · TypeError
Cannot call a class as a function
Error message
Cannot call a class as a function
What it means
Runtime helper _classCallCheck, emitted at the top of every transpiled class constructor when targeting ES<6. It throws a TypeError if `this` is not an instance of the constructor, enforcing the ES2015 rule that classes cannot be invoked without `new`. This reproduces the native class-constructor behavior on older runtimes.
Source
Thrown at packages/babel-helpers/src/helpers/classCallCheck.ts:8
/* @minVersion 7.0.0-beta.0 */
export default function _classCallCheck<T extends object>(
instance: unknown,
Constructor: new (...args: any[]) => T,
): asserts instance is T {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
View on GitHub (pinned to 06b6eae39d)
Solutions
- Invoke the class with `new`: new MyClass().
- If you need callable invocation, wrap it in a function that returns new MyClass().
- If the class is being passed to a framework that calls it, ensure the framework expects a constructor (most do) or adapt with a factory wrapper.
- Target ES2015+ in Babel so _classCallCheck is not emitted and native semantics apply.
Example fix
// before const s = MyClass(); // after const s = new MyClass();
Defensive patterns
Strategy: type-guard
Validate before calling
function isConstructor(fn) {
try { Reflect.construct(fn, []); return true; } catch { return false; }
} Type guard
function isConstructable<T>(fn: unknown): fn is new (...args: any[]) => T {
if (typeof fn !== 'function') return false;
// arrow functions and some builtins are not constructable
try { Reflect.construct(fn, [], function(){}); return true; } catch { return false; }
} Prevention
- Always instantiate classes with new.
- If an API accepts a factory, do not pass a class — wrap it.
- Target ES2015+ to drop _classCallCheck and rely on native semantics.
When it happens
Trigger: At runtime, calling a transpiled class as a plain function: MyClass() instead of new MyClass(), or using Function.prototype.call/apply on it without new.target compatibility. The `instanceof Constructor` check at classCallCheck.ts:7 fails.
Common situations: Calling a class-based component/service without new; passing a class to a callback that invokes it as a function; Reflect.apply on a class; mixing class and factory patterns.
Related errors
- this hasn't been initialised - super() hasn't been called
- Private element is not present on this object
- Object is not async iterable
- Cannot initialize the same private elements twice on an obje
- Class "${name}" cannot be referenced in computed property ke
AI-assisted analysis of babel/babel@06b6eae39d (2026-08-03).
Data as JSON: /data/errors/10ac8a719faa8363.json.
Report an issue: GitHub.