diff --git a/crates/oxc_isolated_declarations/src/class.rs b/crates/oxc_isolated_declarations/src/class.rs index bb38254fdfbf6..231e90b567ab7 100644 --- a/crates/oxc_isolated_declarations/src/class.rs +++ b/crates/oxc_isolated_declarations/src/class.rs @@ -54,40 +54,44 @@ impl<'a> IsolatedDeclarations<'a> { &self, property: &PropertyDefinition<'a>, ) -> ClassElement<'a> { - let type_annotations = if property.accessibility.is_some_and(|a| a.is_private()) { - None - } else { - property - .type_annotation - .as_ref() - .map(|type_annotation| self.ast.copy(type_annotation)) - .or_else(|| { - property - .value - .as_ref() - .and_then(|expr| { - let ts_type = if property.readonly { - self.transform_expression_to_ts_type(expr) - } else { - self.infer_type_from_expression(expr) - }; - if ts_type.is_none() { - self.error(property_must_have_explicit_type(property.key.span())); - } - ts_type - }) - .map(|ts_type| self.ast.ts_type_annotation(SPAN, ts_type)) - }) - }; + let mut type_annotations = None; + let mut value = None; + + if property.accessibility.map_or(true, |a| !a.is_private()) { + if property.type_annotation.is_some() { + type_annotations = self.ast.copy(&property.type_annotation); + } else if let Some(expr) = property.value.as_ref() { + let ts_type = if property.readonly { + // `field = 'string'` remain `field = 'string'` instead of `field: 'string'` + if Self::is_need_to_infer_type_from_expression(expr) { + self.transform_expression_to_ts_type(expr) + } else { + if let Expression::TemplateLiteral(lit) = expr { + value = self + .transform_template_to_string(lit) + .map(Expression::StringLiteral); + } else { + value = Some(self.ast.copy(expr)); + } + None + } + } else { + self.infer_type_from_expression(expr) + }; - // TODO if inferred type_annotations is TSLiteral, it should stand as value, - // so `field = 'string'` remain `field = 'string'` instead of `field: 'string'` + type_annotations = ts_type.map(|t| self.ast.ts_type_annotation(SPAN, t)); + } + + if type_annotations.is_none() && value.is_none() { + self.error(property_must_have_explicit_type(property.key.span())); + } + } self.ast.class_property( property.r#type, property.span, self.ast.copy(&property.key), - None, + value, property.computed, property.r#static, property.declare, diff --git a/crates/oxc_isolated_declarations/tests/snapshots/class.snap b/crates/oxc_isolated_declarations/tests/snapshots/class.snap index 7baebea6d6077..1a8522d645f46 100644 --- a/crates/oxc_isolated_declarations/tests/snapshots/class.snap +++ b/crates/oxc_isolated_declarations/tests/snapshots/class.snap @@ -20,7 +20,7 @@ export declare abstract class Qux { baz(): void; } export declare class Baz { - readonly prop1: 'some string'; + readonly prop1 = 'some string'; prop2: string; private prop3; private prop4;