microsoft / microsoft/TypeScript

Unexpected circularity error when variable is initialized from a class member

Open
#61,606 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: check: Type Circularity Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

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

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgIIAd0BtgLmYAexGQG8AoZK5YAEwC5kBnMKUAcwG5zLq9J2hKAE9GFahJoNmrDtwkBfckvIIscJkzSYc-IiC3jq6AK4AjXcgBioWhmy58+gBQBHE9FEy2IdgEpGe10nYmQAH2QQEywsMl5JZCgIMBMoEiiY+WolFTUNLQAVCBY4iQBlCCwIBEg7HUcCYiZGCrAAHhYfdgA+ZABeSIgAd2RW5z8sqiCG-WbtBz0m8Mjo2IGMrEnkdmTRyura6cWDACFhAGF8CEERccYABTgoAjgsNoAlaqFaDtlfABo9u1OhxumDlhtSglkAB6GHIADqQgA1k9CCYQLRkABGRgAW0IADcUGAABbALQWQgIZE0Ax0FAwITILCEQjoeKSOGIlFojFYgBMLTgxOQAAMyRSAHRHEIGMXIMCEZBwZAIJpgRWkiAkExMEmk-CciTAGDIZySpgy+rHLR9e0rGJ+KHQ6hJFJpR2bY3ZHiu9UGTVJJjRMAPJ4vN6fdVQH4ggFA35dMG9AakBTcH1UJlQc0BkpwG1ygCSABFkIQzZapRUqjUIHUFnKmM6jK6qPnNYWm40SANq7Le1abJjBy5u8Fe2WJlnJKbzROZqF7etVq3Z-7iAQohAtq6VO2+Bq1VcbsIy-0VUXe1L+NchMIpXQ99DO1ee7NL8HQwBtO9nssAF1kAAfhAwYRlaJNQXGF8EkXW0pTgWhaGcBC5SfWgZ0PKhuXOQg8TxHUwBhDF1UI4itUZQgYkIIYOBZUASWVfUUGATUzCSOBkVIkBOIgbiNzdYpf3-B8gMvdChzg5AD2hd1UhIb8sDALYciAA

💻 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground reproduction and compare the two documented workarounds in the getter. Trace why the assignment to result[categoryID] changes the inferred type of application; done means application remains Application | null without either workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.