oxc-project/oxc · warning · OxcDiagnostic
Literals should be exposed using getters.
Error message
Literals should be exposed using getters.
What it means
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.
Source
Thrown at crates/oxc_linter/src/rules/typescript/class_literal_property_style.rs:34
use oxc_str::Str;
use rustc_hash::FxHashSet;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{
AstNode,
context::LintContext,
rule::{DefaultRuleConfig, Rule},
};
fn prefer_field_style_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Literals should be exposed using readonly fields.")
.with_help("Replace this getter with a readonly field initialized to the returned literal.")
.with_label(span)
}
fn prefer_getter_style_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Literals should be exposed using getters.")
.with_help("Replace this readonly literal field with a getter.")
.with_label(span)
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum ClassLiteralPropertyStyleOption {
/// Enforce using readonly fields for literal values.
///
/// Examples of **incorrect** code with this option:
/// ```ts
/// class C {
/// get name() {
/// return "oxc";
/// }
/// }
/// ```
///View on GitHub (pinned to e1e7af627c)
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
Example fix
// before
class Config {
readonly timeoutMs = 5_000;
}
// after
class Config {
get timeoutMs() {
return 5_000;
}
} Defensive patterns
Strategy: validation
Validate before calling
const LITERAL_FIELD = /(readonly|static\s+readonly)\s+\w+\s*=\s*(['"\d]|true|false)\/;/;
for (const line of source.split('\n')) {
if (LITERAL_FIELD.test(line)) fail('readonly literal field; use a getter (style: getters)', line);
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Literals should be exposed using readonly fields.
- Type can be trivially inferred from the initializer
- Expected {getter_key} to be before {setter_key}.
- Prefer using inline type specifiers instead of a top-level t
- Prefer using a top-level type-only import instead of inline
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/f7d59c7e195933ec.
Report an issue: GitHub.