{"record":{"id":"f7d59c7e195933ec","repo":"oxc-project/oxc","slug":"literals-should-be-exposed-using-getters","errorCode":null,"errorMessage":"Literals should be exposed using getters.","messagePattern":"Literals should be exposed using getters\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/typescript/class_literal_property_style.rs","lineNumber":34,"sourceCode":"use 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 {\n    ///   get name() {\n    ///     return \"oxc\";\n    ///   }\n    /// }\n    /// ```\n    ///","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/typescript/class_literal_property_style.rs#L16-L52","documentation":"Warning from typescript/class-literal-property-style via prefer_getter_style_diagnostic() (crates/oxc_linter/src/rules/typescript/class_literal_property_style.rs:34). With the option 'getters', class members exposing literals must be getters: the rule fires on a readonly property initialized to a literal (string, number, boolean, etc.) instead.","triggerScenarios":"oxlint runs with 'class-literal-property-style: [\"error\", \"getters\"]' and the class contains 'readonly x = 1' or 'static readonly label = \"api\"'; the span labels the property declaration.","commonSituations":"Teams standardizing on getters so subclasses can override values; Angular-style components where getter overrides are common; switching the config from the default 'fields' to 'getters' and running against existing classes.","solutions":["Convert the readonly literal field to a getter returning the literal, keeping static/accessibility modifiers","If the value must stay a field (e.g. for destructuring or performance), add an explained inline disable","Reconsider whether 'fields' (the default) fits the codebase better and adjust the config"],"exampleFix":"// before\nclass Config {\n  readonly timeoutMs = 5_000;\n}\n\n// after\nclass Config {\n  get timeoutMs() {\n    return 5_000;\n  }\n}","handlingStrategy":"validation","validationCode":"const LITERAL_FIELD = /(readonly|static\\s+readonly)\\s+\\w+\\s*=\\s*(['\"\\d]|true|false)\\/;/;\nfor (const line of source.split('\\n')) {\n  if (LITERAL_FIELD.test(line)) fail('readonly literal field; use a getter (style: getters)', line);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only enable 'getters' style when subclass overrides are a real requirement","Convert literal fields mechanically with the rule's fixer","Document the convention in the repo's style guide so new code matches"],"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"}