Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(48031): No circularity error for self referential get accessor annotations #48050

Merged
merged 1 commit into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9516,13 +9516,17 @@ namespace ts {
}

let type = resolveTypeOfAccessors(symbol, writing);

if (!popTypeResolution()) {
type = anyType;
if (noImplicitAny) {
const getter = getDeclarationOfKind<AccessorDeclaration>(symbol, SyntaxKind.GetAccessor);
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));
const getter = getDeclarationOfKind<AccessorDeclaration>(symbol, SyntaxKind.GetAccessor);
if (getter) {
if (getEffectiveTypeAnnotationNode(getter)) {
error(getter.name, Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_type_annotation, symbolToString(symbol));
}
else if (noImplicitAny) {
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));
}
}
type = anyType;
}
return type;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tests/cases/compiler/circularGetAccessor.ts(2,9): error TS2502: 'foo' is referenced directly or indirectly in its own type annotation.


==== tests/cases/compiler/circularGetAccessor.ts (1 errors) ====
declare class C {
get foo(): typeof this.foo;
~~~
!!! error TS2502: 'foo' is referenced directly or indirectly in its own type annotation.
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//// [circularGetAccessor.ts]
declare class C {
get foo(): typeof this.foo;
}


//// [circularGetAccessor.js]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/circularGetAccessor.ts ===
declare class C {
>C : Symbol(C, Decl(circularGetAccessor.ts, 0, 0))

get foo(): typeof this.foo;
>foo : Symbol(C.foo, Decl(circularGetAccessor.ts, 0, 17))
>this.foo : Symbol(C.foo, Decl(circularGetAccessor.ts, 0, 17))
>this : Symbol(C, Decl(circularGetAccessor.ts, 0, 0))
>foo : Symbol(C.foo, Decl(circularGetAccessor.ts, 0, 17))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/circularGetAccessor.ts ===
declare class C {
>C : C

get foo(): typeof this.foo;
>foo : any
>this.foo : any
>this : this
>foo : any
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tests/cases/compiler/circularGetAccessor.ts(2,9): error TS2502: 'foo' is referenced directly or indirectly in its own type annotation.


==== tests/cases/compiler/circularGetAccessor.ts (1 errors) ====
declare class C {
get foo(): typeof this.foo;
~~~
!!! error TS2502: 'foo' is referenced directly or indirectly in its own type annotation.
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//// [circularGetAccessor.ts]
declare class C {
get foo(): typeof this.foo;
}


//// [circularGetAccessor.js]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/circularGetAccessor.ts ===
declare class C {
>C : Symbol(C, Decl(circularGetAccessor.ts, 0, 0))

get foo(): typeof this.foo;
>foo : Symbol(C.foo, Decl(circularGetAccessor.ts, 0, 17))
>this.foo : Symbol(C.foo, Decl(circularGetAccessor.ts, 0, 17))
>this : Symbol(C, Decl(circularGetAccessor.ts, 0, 0))
>foo : Symbol(C.foo, Decl(circularGetAccessor.ts, 0, 17))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/circularGetAccessor.ts ===
declare class C {
>C : C

get foo(): typeof this.foo;
>foo : any
>this.foo : any
>this : this
>foo : any
}

5 changes: 5 additions & 0 deletions tests/cases/compiler/circularGetAccessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @noImplicitAny: true, false

declare class C {
get foo(): typeof this.foo;
}
a-tarasyuk marked this conversation as resolved.
Show resolved Hide resolved