Skip to content

Commit 09528c5

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

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

src/compiler/checker.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -9511,16 +9511,19 @@ namespace ts {
95119511
}
95129512

95139513
function getTypeOfAccessorsWorker(symbol: Symbol, writing = false): Type | undefined {
9514+
const getter = getDeclarationOfKind<AccessorDeclaration>(symbol, SyntaxKind.GetAccessor);
95149515
if (!pushTypeResolution(symbol, TypeSystemPropertyName.Type)) {
9516+
if (getter && getEffectiveTypeAnnotationNode(getter)) {
9517+
error(getter.name, Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_type_annotation,
9518+
symbolToString(symbol));
9519+
}
95159520
return errorType;
95169521
}
95179522

95189523
let type = resolveTypeOfAccessors(symbol, writing);
9519-
95209524
if (!popTypeResolution()) {
95219525
type = anyType;
95229526
if (noImplicitAny) {
9523-
const getter = getDeclarationOfKind<AccessorDeclaration>(symbol, SyntaxKind.GetAccessor);
95249527
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));
95259528
}
95269529
}
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,3 @@
1+
declare class C {
2+
get foo(): typeof this.foo;
3+
}

0 commit comments

Comments
 (0)