oxc-project/oxc · error · OxcDiagnostic
Property {parameter} should be declared as a class property.
Error message
Property {parameter} should be declared as a class property. What it means
Raised by typescript/parameter_properties when the rule is configured to prefer explicit class properties and a constructor parameter property (`constructor(private foo: string)`) is found. At the throw site the shorthand declares AND initializes the member implicitly; the project style requires declaring `foo: string` as a class member and assigning it explicitly, which keeps the class shape visible to readers. prefer_class_property_diagnostic fires from check_prefer_class_property for each parameter carrying an accessibility/readonly modifier.
Source
Thrown at crates/oxc_linter/src/rules/typescript/parameter_properties.rs:25
ast::{
AssignmentExpression, AssignmentTarget, Class, ClassElement, Expression, FormalParameter,
MethodDefinition, MethodDefinitionKind, PropertyDefinition, PropertyKey, Statement,
TSAccessibility,
},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_str::Str;
use crate::{
AstNode,
context::{ContextHost, LintContext},
rule::{DefaultRuleConfig, Rule},
};
fn prefer_class_property_diagnostic(parameter: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Property {parameter} should be declared as a class property."))
.with_help("Remove the parameter modifier and declare this member on the class instead.")
.with_label(span)
}
fn prefer_parameter_property_diagnostic(parameter: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Property {parameter} should be declared as a parameter property."))
.with_help("Declare this member as a constructor parameter property and remove the class field plus assignment.")
.with_label(span)
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "kebab-case")]
enum Prefer {
#[default]
ClassProperty,
ParameterProperty,
}
View on GitHub (pinned to e1e7af627c)
Solutions
- Remove the parameter modifier and declare the member as a class property, assigning it in the constructor.
- Change the rule preference to 'parameter-property' if that style is desired.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/typescript/parameter_properties.rs:25 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/8f54aff70a894f09.
Report an issue: GitHub.