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
Activity
- changed the title
[-]Loss of type for class field in for loop[/-][+]Loss of type in for-of loop[/+]on Apr 22, 2025 RyanCavanaugh commented
on Apr 22, 2025 MemberMore actionsSmallest repro I could find. Very weird.
interface Application { id: number; } const result: boolean[] = []; class Test { appArray: Application[] | null = null; foo(): void { if (this.appArray === null) { return; } for (const idx of [0, 1]) { const app = this.appArray[idx]; const categoryID = app.id; result[categoryID] = true; } } }
- changed the title
[-]Loss of type in for-of loop[/-][+]Unexpected circularity error when variable is initialized from a class member[/+]on Apr 22, 2025 RyanCavanaugh commented
on Apr 22, 2025 MemberMore actionsBisects to #45974
RyanCavanaugh commented
on Apr 22, 2025 MemberMore actionsShorter, looking at the PR
const result: boolean[] = []; class Test { appArray: number[] | null = null; foo(): void { if (this.appArray === null) { return; } for (let i = 0; i < 1; i++) { const app = this.appArray[0]; const appRef = app; result[appRef] = true; } } }
- addedBugA bug in TypeScriptA bug in TypeScriptHelp WantedYou can do thisYou can do this
on Apr 22, 2025 - added a commit that references this issue
on Apr 22, 2025 Another workaround is to add the type annotation.
const result: boolean[] = []; class Test { appArray: number[] | null = null; foo(): void { if (this.appArray === null) { return; } for (let i = 0; i < 1; i++) { const app: number = this.appArray[0]; // <- like this const appRef = app; result[appRef] = true; } } }
- addedDomain: 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 exist
on Oct 15, 2025
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
🔎 Search Terms
loss of type, for loop, for-loop, class field
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgIIAd0BtgLmYAexGQG8AoZK5YAEwC5kBnMKUAcwG5zLq9J2hKAE9GFahJoNmrDtwkBfckvIIscJkzSYc-IiC3jq6AK4AjXcgBioWhmy58+gBQBHE9FEy2IdgEpGe10nYmQAH2QQEywsMl5JZCgIMBMoEiiY+WolFTUNLQAVCBY4iQBlCCwIBEg7HUcCYiZGCrAAHhYfdgA+ZABeSIgAd2RW5z8sqiCG-WbtBz0m8Mjo2IGMrEnkdmTRyura6cWDACFhAGF8CEERccYABTgoAjgsNoAlaqFaDtlfABo9u1OhxumDlhtSglkAB6GHIADqQgA1k9CCYQLRkABGRgAW0IADcUGAABbALQWQgIZE0Ax0FAwITILCEQjoeKSOGIlFojFYgBMLTgxOQAAMyRSAHRHEIGMXIMCEZBwZAIJpgRWkiAkExMEmk-CciTAGDIZySpgy+rHLR9e0rGJ+KHQ6hJFJpR2bY3ZHiu9UGTVJJjRMAPJ4vN6fdVQH4ggFA35dMG9AakBTcH1UJlQc0BkpwG1ygCSABFkIQzZapRUqjUIHUFnKmM6jK6qPnNYWm40SANq7Le1abJjBy5u8Fe2WJlnJKbzROZqF7etVq3Z-7iAQohAtq6VO2+Bq1VcbsIy-0VUXe1L+NchMIpXQ99DO1ee7NL8HQwBtO9nssAF1kAAfhAwYRlaJNQXGF8EkXW0pTgWhaGcBC5SfWgZ0PKhuXOQg8TxHUwBhDF1UI4itUZQgYkIIYOBZUASWVfUUGATUzCSOBkVIkBOIgbiNzdYpf3-B8gMvdChzg5AD2hd1UhIb8sDALYciAA
💻 Code
🙁 Actual behavior
applicationhas type:any.FindApplicationreturnsApplication | null, but that gets lost somewhere.🙂 Expected behavior
type for
applicationshould beApplication | null.Additional information about the issue
See comments in code for workarounds and to see another line break type inference.