{"id":"10ac8a719faa8363","repo":"babel/babel","slug":"cannot-call-a-class-as-a-function","errorCode":null,"errorMessage":"Cannot call a class as a function","messagePattern":"Cannot call a class as a function","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/babel-helpers/src/helpers/classCallCheck.ts","lineNumber":8,"sourceCode":"/* @minVersion 7.0.0-beta.0 */\n\nexport default function _classCallCheck<T extends object>(\n  instance: unknown,\n  Constructor: new (...args: any[]) => T,\n): asserts instance is T {\n  if (!(instance instanceof Constructor)) {\n    throw new TypeError(\"Cannot call a class as a function\");\n  }\n}\n","sourceCodeStart":1,"sourceCodeEnd":11,"githubUrl":"https://github.com/babel/babel/blob/06b6eae39da4ce0689fad64e2c48a6375a464208/packages/babel-helpers/src/helpers/classCallCheck.ts#L1-L11","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst s = MyClass();\n// after\nconst s = new MyClass();","handlingStrategy":"type-guard","validationCode":"function isConstructor(fn) {\n  try { Reflect.construct(fn, []); return true; } catch { return false; }\n}","typeGuard":"function isConstructable<T>(fn: unknown): fn is new (...args: any[]) => T {\n  if (typeof fn !== 'function') return false;\n  // arrow functions and some builtins are not constructable\n  try { Reflect.construct(fn, [], function(){}); return true; } catch { return false; }\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["runtime-helper","es-classes","constructor","new-operator"],"analyzedSha":"06b6eae39da4ce0689fad64e2c48a6375a464208","analyzedAt":"2026-08-03T20:13:43.465Z","schemaVersion":2}