Repository navigation
Unexpected circularity error when variable is initialized from a class member #61606
Copy link
Copy link
Open
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Type Circularitystackoverflows caused by circularities, or unexpected errors where no circularity appears to existstackoverflows caused by circularities, or unexpected errors where no circularity appears to existHelp WantedYou can do thisYou can do this
Milestone
Description
🔎 Search Terms
loss of type, for loop, for-loop, class field
🕗 Version & Regression Information
- This changed between versions ______ and _______
- This changed in commit or PR _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
⏯ Playground Link
💻 Code
interface Application {
id: string;
category: {
id: string;
}
}
class Applications {
public FindApplication(query: string): Application | null {
return null;
}
}
class Test {
SelectedApplications: Set<string> = new Set();
Applications: Applications | null = null;
get SelectedApplicationsByCategory(): Partial<Record<string, Set<string>>> | null {
// Workaround 1: move this block inside for loop
// Workaround 2: Save this.Applications to a const then use that
if (this.Applications === null) {
return null;
}
const result: Partial<Record<string, Set<string>>> = {};
for (const applicationID of this.SelectedApplications) {
const application = this.Applications.FindApplication(applicationID);
if (application === null) {
continue;
}
const categoryID = application.category.id;
const applications = result[categoryID] ?? new Set<string>();
applications.add(application.id);
// Comment/uncomment the following line to see it break/unbreak
result[categoryID] = applications;
}
return result;
}
}🙁 Actual behavior
application has type: any. FindApplication returns Application | null, but that gets lost somewhere.
🙂 Expected behavior
type for application should be Application | null.
Additional information about the issue
See comments in code for workarounds and to see another line break type inference.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Type Circularitystackoverflows caused by circularities, or unexpected errors where no circularity appears to existstackoverflows caused by circularities, or unexpected errors where no circularity appears to existHelp WantedYou can do thisYou can do this
Activity
[-]Loss of type for class field in for loop[/-][+]Loss of type in for-of loop[/+]RyanCavanaugh commentedon Apr 22, 2025
Smallest repro I could find. Very weird.
[-]Loss of type in for-of loop[/-][+]Unexpected circularity error when variable is initialized from a class member[/+]RyanCavanaugh commentedon Apr 22, 2025
Bisects to #45974
RyanCavanaugh commentedon Apr 22, 2025
Shorter, looking at the PR
Check target equivalence in isMatchingReference before computing prop…
carlosalmonte04 commentedon Jul 7, 2025
Another workaround is to add the type annotation.