oxc-project/oxc · warning · OxcDiagnostic
Literals should be exposed using readonly fields.
Error message
Literals should be exposed using readonly fields.
What it means
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'.
Source
Thrown at crates/oxc_linter/src/rules/typescript/class_literal_property_style.rs:28
};
use oxc_ast_visit::VisitJs;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::ScopeFlags;
use oxc_span::{GetSpan, Span};
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 {View on GitHub (pinned to e1e7af627c)
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
Example fix
// before
class Config {
get timeoutMs() {
return 5_000;
}
}
// after
class Config {
readonly timeoutMs = 5_000;
} Defensive patterns
Strategy: validation
Validate before calling
const LITERAL_GETTER = /get\s+\w+\(\)\s*\{\s*return\s+(['"\d]|true|false|null)\/;/;
for (const line of source.split('\n')) {
if (LITERAL_GETTER.test(line)) fail('getter returns a literal; use a readonly field', line);
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Literals should be exposed using getters.
- 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/2243ac030541c185.
Report an issue: GitHub.