-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
TS7022 (circular reference) on simple assignments in a loop #59074
Comments
I'm fixing the weird inconsistent quick info display here: #59075 |
Those issues look similar but I'm not sure the root cause is the same. The supposed circularity in my repro code is being caused by an empty if statement which is essentially dead code here. I assume it's narrowing something internally but given that it's not affecting the control flow at all (can be removed with zero consequences to the runtime), I don't see a good reason for this narrowing to be occurring. (Well, in the code that sparked the issue the if statement is not exactly dead code, but it's still unrelated to the variable that's getting the error.)
Doesn't that make it worse, though? The problem is in the extraneous error here, the tooltip has the correct type... |
It requests type of
The tooltip has the correct type somewhat accidentally here. An encountered circularity returns That said, this alternative patch doesn't change anything in the existing test suite and it "fixes" your particular circularity issue: diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts
index a965c51d22..640a0216c5 100644
--- a/src/compiler/checker.ts
+++ b/src/compiler/checker.ts
@@ -12055,6 +12055,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (isBindingElement(declaration) && checkMode === CheckMode.Contextual) {
return errorType;
}
+ if (checkMode && checkMode & CheckMode.TypeOnly) {
+ return anyType;
+ }
return reportCircularityError(symbol);
}
let type: Type;
@@ -12137,6 +12140,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (isBindingElement(declaration) && checkMode === CheckMode.Contextual) {
return type;
}
+ if (checkMode && checkMode & CheckMode.TypeOnly) {
+ return anyType;
+ }
return reportCircularityError(symbol);
}
return type; But despite that, I think it's rather risky to allow this to propagate freely because the result propagates. Similarly, I also checked that it wouldn't have a perceivable bad impact on this scenario with an auto type: declare const line: string;
let test;
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (test === char) {}
const alsoChar = char;
test = alsoChar;
} FWIW, I'm not saying that the circularity avoidance couldn't be improved for some of those cases. I feel like the change I proposed is good regardless of that. |
#56753 is being superceded by #59183. This is a true circularity for basically the same reason as #33191 (comment). |
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
🔎 Search Terms
TS7022, implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer, circular reference, circular type
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=5.5.2#code/CYUwxgNghgTiAEYD2A7AzgF3hAlikAXPJjHgOYDcAUBCFhiJkSefAD7woCuEE8AvJx4RqVAGZIY8ABS0sOAfAAMFeAoA82PCAB0tFGQwALVTgDUZgJTwA3lXiJUmREdiLc+ANo4AutQc4YjIMzvxhLrDWNgC+9o7oWFAQaEgAwq5SgmAZ-vAhWIJJKemw1LFAA
💻 Code
🙁 Actual behavior
The following error is triggered:
'alsoChar' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022)
🙂 Expected behavior
No errors.
char
is clearly a string andalsoChar
is just a copy of it, so there shouldn't be any type confusion and/or circularity.test
isn't even read from, except in the empty if statement.Additional information about the issue
if
on line 6 fixes the error.if
on line 6 byif (test !== char) {}
also produces the error, butif (test! < char) {}
doesn't.let test: string | undefined = undefined;
also produces the error, butlet test: string | undefined;
doesn't.This version also reproduces the issue:
Same for this one:
But this one doesn't:
Another interesting point is that despite the error, the tooltip does show
const alsoChar: string
as expected. This is recent, though: v5.3.3 shows the type asany
, whereas v5.4.5 shows it asstring
.The text was updated successfully, but these errors were encountered: