{"record":{"id":"74853a9708cc36fe","repo":"swc-project/swc","slug":"decorating-class-property-failed-please-ensure-th","errorCode":null,"errorMessage":"Decorating class property failed. Please ensure that proposal-class-properties is enabled and set to use loose mode. To use proposal-class-properties in spec mode with decorators, wait for the next major version of decorators in stage 2.","messagePattern":"Decorating class property failed\\. Please ensure that proposal-class-properties is enabled and set to use loose mode\\. To use proposal-class-properties in spec mode with decorators, wait for the next major version of decorators in stage 2\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_transforms_base/src/helpers/generated/_initializer_warning_helper.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::initializer_warning_helper,\n    local: \"_initializer_warning_helper\",\n    import_path: \"@swc/helpers/_/_initializer_warning_helper\",\n    #[cfg(feature = \"inline-helpers\")]\n    source: r#\"function _initializer_warning_helper(descriptor, context) {\n    throw new Error(\n        \"Decorating class property failed. Please ensure that \"\n            + \"proposal-class-properties is enabled and set to use loose mode. \"\n            + \"To use proposal-class-properties in spec mode with decorators, wait for \"\n            + \"the next major version of decorators in stage 2.\"\n    );\n}\n\"#,\n    #[cfg(feature = \"inline-helpers\")]\n    deps: super::HelperBitmap::from_bits(0x00000000000000000100000000000000),\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":29,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_transforms_base/src/helpers/generated/_initializer_warning_helper.rs#L1-L29","documentation":"SWC injects this helper when legacy (stage-1) decorators are compiled together with class properties in spec mode, a combination the legacy decorator runtime cannot support. Instead of implementing it, the compiled code calls `_initializer_warning_helper`, which unconditionally throws this Error the moment the decorated field initializer would run. The message is really a build-configuration diagnostic surfaced at runtime: it instructs you to compile class fields in loose mode.","triggerScenarios":"Source like `class C { @dec x = 1; }` compiled with legacy decorators enabled (`.swcrc` `jsc.transform.legacyDecorator: true` or equivalent) while class fields are transformed in spec mode (`jsc.assumptions.setPublicClassFields` not set, env not in loose mode). The error fires as soon as the decorated class is defined or instantiated in the emitted output.","commonSituations":"Next.js or raw .swcrc configs enabling legacy decorators for TypeORM/class-validator/older MobX while leaving SWC's default spec-mode class fields; migrating from Babel where `@babel/plugin-proposal-decorators {legacy: true}` was paired with `@babel/plugin-proposal-class-properties {loose: true}` and the loose half of the pair was lost in translation.","solutions":["In .swcrc, pair legacy decorators with loose class fields: set `jsc.transform.legacyDecorator: true` together with `jsc.assumptions.setPublicClassFields: true` (or compile with `env.mode: \"loose\"`).","If legacy decorators are not required, switch to standard decorators (`jsc.transform.decoratorVersion: \"2022-03\"` or later) and update decorator-using libraries to versions that support them.","Upgrade the decorator library (TypeORM 0.3+, class-transformer with modern experimentalDecorators alternatives, MobX 10+) so legacy mode is no longer needed.","As a last resort, remove decorators from field initializers and apply their logic in the constructor."],"exampleFix":"// before (.swcrc)\n{\n  \"jsc\": {\n    \"transform\": { \"legacyDecorator\": true }\n  }\n}\n// throws at runtime: Decorating class property failed...\n\n// after (.swcrc)\n{\n  \"jsc\": {\n    \"transform\": { \"legacyDecorator\": true },\n    \"assumptions\": { \"setPublicClassFields\": true }\n  }\n}","handlingStrategy":"validation","validationCode":"// Pre-build config check (Node): fail CI if legacy decorators are enabled without loose class fields.\nconst cfg = require('./.swcrc');\nconst jsc = (Array.isArray(cfg) ? cfg[0] : cfg).jsc;\nconst legacy = jsc?.transform?.legacyDecorator || jsc?.experimental?.decorators === true;\nconst looseFields = jsc?.assumptions?.setPublicClassFields === true || jsc?.transform?.reactImplicitReturn ?? false;\nif (legacy && !jsc?.assumptions?.setPublicClassFields) {\n  throw new Error('legacyDecorator requires jsc.assumptions.setPublicClassFields: true');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Whenever you enable `legacyDecorator` in .swcrc, add `setPublicClassFields: true` in the same commit and review it together.","Add a smoke test that imports and instantiates every decorated class so a bad decorator/class-fields combination fails in CI, not production.","Pin your swc_core/@swc/core versions and re-run snapshot fixtures after upgrades — decorator handling changed across SWC releases."],"tags":["decorators","class-properties","legacy-decorators","swc","build-config"],"backgroundTag":"decorators-class-fields-loose-mode","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}