microsoft / microsoft/TypeScript

Mixin classes with private/protected constructors are subclassable

Offen
#42,264 3 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: classes
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Bug Report

🔎 Search Terms

mixin, private constructor

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about Classes.
Versions tried:

  • 3.7.4
  • 4.1.3
  • Nightly
⏯ Playground Link

Playground link with relevant code

💻 Code
function PreventSubclassingMixin<TBase extends new(...args: any[]) => any>(Base: TBase) {
  return class NonSubclassable extends Base {
    private constructor(...args: any[]) {
      super();
    }
  }
}

class Base {
  constructor() {}
}

class NonSubclassableThroughExtension extends Base {
  private constructor() {
    super();
  }
}

const NonSubclassableThroughMixin = PreventSubclassingMixin(Base);

new NonSubclassableThroughExtension(); // Constructor of class 'NonSubclassableThroughExtension' is private and only accessible within the class declaration.(2673)
new NonSubclassableThroughMixin(); // Doesn't throw an error
🙁 Actual behavior

NonSubclassableThroughExtension correctly throw an error — I've extended a superclass Base, which has a publicly accessible constructor, and provided a private constructor in the derived class. I can't access the derived class' constructor anymore:

new NonSubclassableThroughExtension(); // Constructor of class 'NonSubclassableThroughExtension' is private and only accessible within the class declaration.(2673)

The PreventSubclassingMixin supposedly works the same — it creates a class expression that extends the Base class with a public constructor and defines a private constructor, and returns it. The returned derived mixin class is supposedly identical to the example above. However, if I apply the mixin to the Base: const NonSubclassableThroughMixin = PreventSubclassingMixin(Base), and try to initialize it, Typescript won't throw any error.

🙂 Expected behavior

new NonSubclassableThroughMixin() should throw a Constructor of class 'NonSubclassableThroughExtension' is private and only accessible within the class declaration.(2673).

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verknüpften TypeScript Playground und reproduziere die beiden Konstruktorzugriffsprüfungen in den bereitgestellten Mixin- und Direkt-Erweiterungsbeispielen. Verfolge die Konstruktorzugänglichkeit für den vom Mixin zurückgegebenen Klassenausdruck und füge dann einen Regressionstest hinzu oder aktualisiere ihn, sodass new NonSubclassableThroughMixin() dieselbe Diagnose für einen privaten Konstruktor erhält wie die direkte Unterklasse.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.