Skip to content

Computing variance causes circularity error #53470

Closed
@gabritto

Description

@gabritto

Bug Report

Sometimes we start to compute variances while we're in the middle of computing the types for e.g. a property, and then during variance computation, we try to compute the type for the same property again, so we error on that circularity. However, those circularity errors could be avoided if we were to "restart" when we start computing variances, i.e. for purposes of circularity detection, ignore the types we were previously computing. This becomes a larger problem when the error is or not present depending on the order in which things are declared, as in the example below.

This is a fork of issue #52813, which was fixed for the specific case where the thing that triggers a variance computation is a type assertion, and the fix implemented (#53261) was to defer the type comparability done for type assertions, to delay triggering variance computations that aren't immediately needed.
However, in scenarios where variance computation is triggered in other ways, the circularity error is still present and could be avoided:

🔎 Search Terms

variance circularity

⏯ Playground Link

Playground link with relevant code

💻 Code

class Bar<T> {
    num!: number;    // Swap to remove error
    Value = callme(this).num;
    Field: number = callme(this).num;
}
declare function callme(x: Bar<any>): Bar<any>;
declare function callme(x: object): string;

🙁 Actual behavior

Error: 'Value' 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 error.

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions