fix(isolated_declarations): inferring literal types for readonly class fileds (#4027)

This commit is contained in:
Egor Blinov 2024-07-02 17:00:37 +02:00 committed by GitHub
parent b66ad0b675
commit da628399fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 3 deletions

View file

@ -66,7 +66,11 @@ impl<'a> IsolatedDeclarations<'a> {
.value
.as_ref()
.and_then(|expr| {
let ts_type = self.infer_type_from_expression(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()));
}
@ -76,6 +80,9 @@ impl<'a> IsolatedDeclarations<'a> {
})
};
// TODO if inferred type_annotations is TSLiteral, it should stand as value,
// so `field = 'string'` remain `field = 'string'` instead of `field: 'string'`
self.ast.class_property(
property.r#type,
property.span,

View file

@ -6,10 +6,19 @@ export class Bar {
public constructor(a: number = 0) {}
}
export class Zoo { foo<F>(f: F): F { return f } }
export class Zoo {
foo<F>(f: F): F {
return f;
}
}
export abstract class Qux {
abstract foo(): void;
bar(): void {}
baz(): void {}
}
}
export class Baz {
readonly prop1 = 'some string';
prop2 = 'another string';
}

View file

@ -18,3 +18,7 @@ export declare abstract class Qux {
bar(): void;
baz(): void;
}
export declare class Baz {
readonly prop1: 'some string';
prop2: string;
}