Skip to content

Commit 082ea83

Browse files
committed
fix(48031): show circularity error for self referential get accessor annotations
1 parent 0043abe commit 082ea83

10 files changed

+92
-5
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9516,13 +9516,17 @@ namespace ts {
95169516
}
95179517

95189518
let type = resolveTypeOfAccessors(symbol, writing);
9519-
95209519
if (!popTypeResolution()) {
9521-
type = anyType;
9522-
if (noImplicitAny) {
9523-
const getter = getDeclarationOfKind<AccessorDeclaration>(symbol, SyntaxKind.GetAccessor);
9524-
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));
9520+
const getter = getDeclarationOfKind<AccessorDeclaration>(symbol, SyntaxKind.GetAccessor);
9521+
if (getter) {
9522+
if (getEffectiveTypeAnnotationNode(getter)) {
9523+
error(getter.name, Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_type_annotation, symbolToString(symbol));
9524+
}
9525+
else if (noImplicitAny) {
9526+
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));
9527+
}
95259528
}
9529+
type = anyType;
95269530
}
95279531
return type;
95289532
}
Lines changed: 10 additions & 0 deletions
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+
Lines changed: 7 additions & 0 deletions
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]
Lines changed: 11 additions & 0 deletions
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+
Lines changed: 11 additions & 0 deletions
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+
Lines changed: 10 additions & 0 deletions
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+
Lines changed: 7 additions & 0 deletions
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]
Lines changed: 11 additions & 0 deletions
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+
Lines changed: 11 additions & 0 deletions
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+
Lines changed: 5 additions & 0 deletions
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)