microsoft / microsoft/TypeScript

Enables `implements` on abstract classes with protected members (usefull for generic mixins)

Offen
#61,161 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

🔍 Search Terms

implement abstract class with protected

✅ Viability Checklist
⭐ Suggestion

Enable doing the following (currently this raises an error):

abstract class Base {
  protected abstract foo(): void
}

class B implements Base {
  protected foo() {}
}
📃 Motivating Example

This would be useful to declare "protected" interfaces, when we can't use extends as the child class is already extending another.

// interface
abstract class Base {
  protected abstract foo(): void
}

class C {}

class B extends C implements Base {
  protected foo() {}
}

Such need is frequent when using generic mixins: indeed, type manipulations inside generic functions (and mixins) are often quite troublesome, with unwanted type expansions/generalization (?). Then this is often better explicitly describe the returning type.

To do so, abstract class is the only way to properly describe the properties added by the mixin. However, if such class has protected properties, this raises TS error as classes with protected properties are no longer duck typing but "nominal" (?).

type Cstr<T> = new(...args:any[]) => T;
type Merge<A extends abstract new(...args:any[])=>any,
           B extends new(...args:any[])=>any
          > = Omit<A & B, "new"> & (new() => (InstanceType<A> & InstanceType<B>))

function foo<T extends Cstr<C>>(klass: T): Merge<typeof Base, T> {
  return class B extends klass implements Base {
    protected foo() {}
  }
}

class X extends foo(C) {}
💻 Use Cases
  1. What do you want to use this for?

Easily describing generic mixins return type.

  1. What shortcomings exist with current approaches?

Can't describe protected properties.

  1. What workarounds are you using in the meantime?

Dirty casting, removing some type checks inside the generic mixin.

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

Das Issue enthält keinen Datei- oder Testpfad. Beginne damit, die Typprüfungslogik und die Tests für die Kompatibilität von Klassen mit implements und geschützten Membern zu finden, und vergleiche anschließend die Beispiele für abstrakte Klassen und generische Mixins. Als abgeschlossen gilt die Aufgabe, wenn die Beispiele ohne Änderung des erzeugten JavaScript typgeprüft werden und eine Regressionstestabdeckung vorhanden ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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