{"record":{"id":"2243ac030541c185","repo":"oxc-project/oxc","slug":"literals-should-be-exposed-using-readonly-fields","errorCode":null,"errorMessage":"Literals should be exposed using readonly fields.","messagePattern":"Literals should be exposed using readonly fields\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/typescript/class_literal_property_style.rs","lineNumber":28,"sourceCode":"};\nuse oxc_ast_visit::VisitJs;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::ScopeFlags;\nuse oxc_span::{GetSpan, Span};\nuse oxc_str::Str;\nuse rustc_hash::FxHashSet;\nuse schemars::JsonSchema;\nuse serde::{Deserialize, Serialize};\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n};\n\nfn prefer_field_style_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Literals should be exposed using readonly fields.\")\n        .with_help(\"Replace this getter with a readonly field initialized to the returned literal.\")\n        .with_label(span)\n}\n\nfn prefer_getter_style_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Literals should be exposed using getters.\")\n        .with_help(\"Replace this readonly literal field with a getter.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]\n#[serde(rename_all = \"lowercase\")]\npub enum ClassLiteralPropertyStyleOption {\n    /// Enforce using readonly fields for literal values.\n    ///\n    /// Examples of **incorrect** code with this option:\n    /// ```ts\n    /// class C {","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/typescript/class_literal_property_style.rs#L10-L46","documentation":"Warning from typescript/class-literal-property-style via prefer_field_style_diagnostic() (crates/oxc_linter/src/rules/typescript/class_literal_property_style.rs:28). With the option 'fields' (the default), class members that expose a literal must be readonly fields, not getters: the rule fires on a getter whose body is a single return of a literal without referencing 'this'.","triggerScenarios":"oxlint runs with class-literal-property-style set to (or defaulting to) 'fields' and encounters 'class C { get x() { return 1; } }' or a getter returning a string/boolean/null literal; the span labels the getter.","commonSituations":"Codebases converting Java-style accessor patterns to TypeScript; refactors that left trivial getters in place; enabling the rule on legacy class hierarchies where getters were used for DI-friendly overrides.","solutions":["Replace the getter with a readonly field initialized to the literal, preserving modifiers like static/abstract/accessibility","If derived classes need to override the member, keep the getter and disable the rule for that member with an explained comment","If your convention genuinely prefers getters, set the option to 'getters' in .oxlintrc.json"],"exampleFix":"// before\nclass Config {\n  get timeoutMs() {\n    return 5_000;\n  }\n}\n\n// after\nclass Config {\n  readonly timeoutMs = 5_000;\n}","handlingStrategy":"validation","validationCode":"const LITERAL_GETTER = /get\\s+\\w+\\(\\)\\s*\\{\\s*return\\s+(['\"\\d]|true|false|null)\\/;/;\nfor (const line of source.split('\\n')) {\n  if (LITERAL_GETTER.test(line)) fail('getter returns a literal; use a readonly field', line);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pick one class-literal-property-style value in the shared config and stick to it","Write readonly fields by default for constant members","Run oxlint --fix on the rule when adopting it, in a dedicated mechanical commit"],"tags":["typescript","oxlint","lint","class","getter","readonly","style"],"backgroundTag":"class-literal-property-style","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}