{"record":{"id":"fc9c42a4f7dbfbf3","repo":"swc-project/swc","slug":"cannot-instantiate-an-arrow-function","errorCode":null,"errorMessage":"Cannot instantiate an arrow function","messagePattern":"Cannot instantiate an arrow function","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_transforms_base/src/helpers/generated/_new_arrow_check.rs","lineNumber":11,"sourceCode":"// This file is generated by `cargo codegen helpers`. DO NOT MODIFY.\n\nuse super::{HelperDef, HelperName};\n\npub const DEF: HelperDef = HelperDef {\n    name: HelperName::new_arrow_check,\n    local: \"_new_arrow_check\",\n    import_path: \"@swc/helpers/_/_new_arrow_check\",\n    #[cfg(feature = \"inline-helpers\")]\n    source: r#\"function _new_arrow_check(innerThis, boundThis) {\n    if (innerThis !== boundThis) throw new TypeError(\"Cannot instantiate an arrow function\");\n}\n\"#,\n    #[cfg(feature = \"inline-helpers\")]\n    deps: super::HelperBitmap::from_bits(0x00000000000000020000000000000000),\n};\n\n#[cfg(feature = \"inline-helpers\")]\npub fn stmts() -> &'static [swc_ecma_ast::Stmt] {\n    static STMTS: once_cell::sync::Lazy<Vec<swc_ecma_ast::Stmt>> =\n        once_cell::sync::Lazy::new(|| super::super::parse(DEF.source, DEF.import_path));\n    &STMTS\n}\n","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_transforms_base/src/helpers/generated/_new_arrow_check.rs#L1-L24","documentation":"Native arrow functions are not constructible, so `new arrowFn()` throws a TypeError in every engine. When SWC lowers async arrow functions (ES2017 async_to_generator) or spec-mode class-property arrows into ordinary functions, the output would otherwise silently accept `new`; SWC inserts `_new_arrow_check(this, _this)` at the top of the lowered body to preserve native semantics and throw `Cannot instantiate an arrow function`.","triggerScenarios":"Applying `new` or `Reflect.construct` to a value that is an async arrow function after lowering, e.g. `const C = async () => {}; new C()`, or DI/event frameworks that do `new (resolve(key))()` where the resolved factory is an async arrow.","commonSituations":"Refactoring a class constructor into an async arrow while call sites still use `new`; generic factories or test utilities that construct any registered callable; wrapping constructors with arrow-based decorators and then constructing the result.","solutions":["Replace the arrow with a class expression or a regular function expression if the value must be constructible.","Audit call sites that apply `new` to dynamic values and make sure only classes/constructor functions reach them.","If the logic must stay an arrow, expose it via a wrapper class whose constructor delegates to the arrow."],"exampleFix":"// before\nconst HttpClient = async (base) => { /* ... */ };\nconst client = new HttpClient('/api'); // TypeError: Cannot instantiate an arrow function\n\n// after\nclass HttpClient {\n  constructor(base) { this.base = base; }\n  async request(path) { /* ... */ }\n}\nconst client = new HttpClient('/api');","handlingStrategy":"type-guard","validationCode":"// Before calling `new` on a dynamic value, check it is plausibly constructible.\n// Native (and lowered) arrows lack a `prototype` property or are async/generator functions.\nfunction isConstructible(fn) {\n  if (typeof fn !== 'function') return false;\n  if (!fn.prototype) return false; // native arrows, async and generator functions\n  return true;\n}\nconst C = registry.get('client');\nconst client = isConstructible(C) ? new C() : C();","typeGuard":"const isConstructible = (fn: unknown): fn is new (...args: never[]) => unknown =>\n  typeof fn === 'function' && (fn as { prototype?: unknown }).prototype != null;","tryCatchPattern":"try {\n  const inst = new Factory();\n} catch (err) {\n  if (err instanceof TypeError && /arrow function/.test(err.message)) {\n    // Factory is an arrow function — call it instead of constructing\n    const inst = Factory();\n  } else throw err;\n}","preventionTips":["Never register arrow functions with DI containers or factories that apply `new`.","If a value must be constructed, define it with `class` — arrows are never constructible by design.","Note: after transpilation an arrow becomes a plain function, so `f.prototype === undefined` only catches native arrows; author-time discipline is the reliable guard."],"tags":["arrow-function","constructor","new","async","es2017"],"backgroundTag":"new-on-arrow-function","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}