Skip to content

Commit f4d76e5

Browse files
authored
fix(48031): show circularity error for self referential get accessor annotations (microsoft#48050)
1 parent e742625 commit f4d76e5

10 files changed

+92
-5
lines changed

Diff for: src/compiler/checker.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -9628,13 +9628,17 @@ namespace ts {
96289628
}
96299629

96309630
let type = resolveTypeOfAccessors(symbol, writing);
9631-
96329631
if (!popTypeResolution()) {
9633-
type = anyType;
9634-
if (noImplicitAny) {
9635-
const getter = getDeclarationOfKind<AccessorDeclaration>(symbol, SyntaxKind.GetAccessor);
9636-
error(getter, Diagnostics._0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions, symbolToString(symbol));
9632+
const getter = getDeclarationOfKind<AccessorDeclaration>(symbol, SyntaxKind.GetAccessor);
9633+
if (getter) {
9634+
if (getEffectiveTypeAnnotationNode(getter)) {
9635+
error(getter.name, Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_type_annotation, symbolToString(symbol));
9636+
}
9637+
else if (noImplicitAny) {
9638+
error(getter, Diagnostics._0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions, symbolToString(symbol));
9639+
}
96379640
}
9641+
type = anyType;
96389642
}
96399643
return type;
96409644
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/circularGetAccessor.ts(2,9): error TS2502: 'foo' is referenced directly or indirectly in its own type annotation.
2+
3+
4+
==== tests/cases/compiler/circularGetAccessor.ts (1 errors) ====
5+
declare class C {
6+
get foo(): typeof this.foo;
7+
~~~
8+
!!! error TS2502: 'foo' is referenced directly or indirectly in its own type annotation.
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [circularGetAccessor.ts]
2+
declare class C {
3+
get foo(): typeof this.foo;
4+
}
5+
6+
7+
//// [circularGetAccessor.js]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/circularGetAccessor.ts ===
2+
declare class C {
3+
>C : Symbol(C, Decl(circularGetAccessor.ts, 0, 0))
4+
5+
get foo(): typeof this.foo;
6+
>foo : Symbol(C.foo, Decl(circularGetAccessor.ts, 0, 17))
7+
>this.foo : Symbol(C.foo, Decl(circularGetAccessor.ts, 0, 17))
8+
>this : Symbol(C, Decl(circularGetAccessor.ts, 0, 0))
9+
>foo : Symbol(C.foo, Decl(circularGetAccessor.ts, 0, 17))
10+
}
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/circularGetAccessor.ts ===
2+
declare class C {
3+
>C : C
4+
5+
get foo(): typeof this.foo;
6+
>foo : any
7+
>this.foo : any
8+
>this : this
9+
>foo : any
10+
}
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/circularGetAccessor.ts(2,9): error TS2502: 'foo' is referenced directly or indirectly in its own type annotation.
2+
3+
4+
==== tests/cases/compiler/circularGetAccessor.ts (1 errors) ====
5+
declare class C {
6+
get foo(): typeof this.foo;
7+
~~~
8+
!!! error TS2502: 'foo' is referenced directly or indirectly in its own type annotation.
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [circularGetAccessor.ts]
2+
declare class C {
3+
get foo(): typeof this.foo;
4+
}
5+
6+
7+
//// [circularGetAccessor.js]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/circularGetAccessor.ts ===
2+
declare class C {
3+
>C : Symbol(C, Decl(circularGetAccessor.ts, 0, 0))
4+
5+
get foo(): typeof this.foo;
6+
>foo : Symbol(C.foo, Decl(circularGetAccessor.ts, 0, 17))
7+
>this.foo : Symbol(C.foo, Decl(circularGetAccessor.ts, 0, 17))
8+
>this : Symbol(C, Decl(circularGetAccessor.ts, 0, 0))
9+
>foo : Symbol(C.foo, Decl(circularGetAccessor.ts, 0, 17))
10+
}
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/circularGetAccessor.ts ===
2+
declare class C {
3+
>C : C
4+
5+
get foo(): typeof this.foo;
6+
>foo : any
7+
>this.foo : any
8+
>this : this
9+
>foo : any
10+
}
11+

Diff for: tests/cases/compiler/circularGetAccessor.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @noImplicitAny: true, false
2+
3+
declare class C {
4+
get foo(): typeof this.foo;
5+
}

0 commit comments

Comments
 (0)