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

  1. Invoke the class with `new`: new MyClass().
  2. If you need callable invocation, wrap it in a function that returns new MyClass().
  3. 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.
  4. 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

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


AI-assisted analysis of babel/babel@06b6eae39d (2026-08-03). Data as JSON: /data/errors/10ac8a719faa8363.json. Report an issue: GitHub.