Skip to content

Non-first occurrence of circular class instantiation is always silently allowedΒ #59606

Closed as not planned
@Andarist

Description

@Andarist

πŸ”Ž Search Terms

variance class expression instantiation reference

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.0-dev.20240812#code/MYGwhgzhAECCA8AVaBeaA7ArgWwEYFMAnAPmgG8AoaaANzBE3wEIAuaRAbiumAAsBLEABNW0AC4BPAA74A9gDM4AOgBC8BImLEO0APS7oRQrMIRufQUIBMoyTIXK1GrTv3QIYbPmiQfuWTTeuJhi0EKy+BDoAOShRiYU3BBiYGL8wNAqqDzgUJlIhgAeYvjoQjCw2bCklNTUUmCEpWKinNwAvlztiaCQFVYFaFh4RDXcdAzMbG3UFsK20nKKsFaq6gPDBCTa5gLCNmx2S3CrTgOa2noGHl4+FUpzQtAAFGBlyo9WAJTQwaHhkRicUIxkIiWoyVS6Uy2V6eTUyHwxVK5ROVSsYzq0AaTXQLWmXGonQo3SAA

πŸ’» Code

class A<T = number> {
  value!: T;
  child!: typeof A.B<A<T>>; // errors
  child2!: typeof A.B<A<T>>; // same as above but doesn't error

  static B = class B<T extends A = A> {
    parent!: T;
  };
}

class A2<T = number> {
  value!: T;
  child!: typeof A2.B<A2<number>>;
  child2!: typeof A2.B<A2<T>>; // same as A.child (and A.child2) but doesn't error

  static B = class B<T extends A2 = A2> {
    parent!: T;
  };
}

πŸ™ Actual behavior

child2 in both classes is free of errors

πŸ™‚ Expected behavior

both child2 properties should contain errors (like child in A)

Additional information about the issue

This is related to the fact that child2 reenters getVariancesWorker for the same symbol, so it returns with emptyArray that was eagerly set as links.variances on the first entry of this function. This, in turn, is a signal for structuredTypeRelatedToWorker to return Ternary.Unknown.

Since this is truly a circular situation, I think this is effectively a design limitation. I'm only reporting this for documentation purposes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions