microsoft / microsoft/TypeScript

Support forwarding generic type in extending class

Offen
#41,719 0 Kommentare 12 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
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Search Terms

generic forward extends default

Suggestion

Have a shortcut syntax to make the generic of an extending class the same type as its Parent generic.

Current

class Parent <T extends any = any> {}
class Child <S extends any = any> extends Parent <S> {}

Suggestion

class Parent <T extends any = any> {}
class Child <infer S> extends Parent <S> {}

Use Cases

A default generic is useful to typescript users b

interface Proto = {}
class SomeClass <T extends Proto = Proto> {}

It can be extended like this:

class ExtendedClass extends SomeClass {}

but with this the author of ExtendedClass robs futher extenders of the possibility to extend the T generic of SomeClass. To allow further extension the author would need to add following construct.

class ExtendedClass <T extends Proto = Proto> extends SomeClass<T> {}

This is quite bothersome to the author of ExtendedClass. It requires the import of Proto and is easy to forget.
To make it a little bit easier I thought it might be a good idea to have this special syntax.

Examples

type Events = {
   open: () => {}
   close: () => {}
}

class System <TEvents extends Events = Events> {
   on <TEvent extends keyof TEvents> (event: TEvent, listener: TEvents[TEvent]): this { return this }
   off <TEvent extends keyof TEvents> (event: TEvent, listener: TEvents[TEvent]): this { return this }
}

class CorpSystem <infer TEvents> extends System <TEvents> {}

type SectionEvents = {
   stall: () => {}
} & Events

class SectionSystem <TEvents extends SectionEvents = SectionEvents> extends CorpSystem<TEvents> {}

const s = new SectionSystem()
s.on('stall', () => {})

As probably clear in the example, this thought came up when dealing with Node's EventEmitter but I think it can be useful to pretty much any use of extending classes with generics.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 der vorgeschlagenen infer-Syntax und den Beispielen Parent, Child, System, CorpSystem und SectionSystem im Issue. Bestimme, wie generisches Weiterleiten mit Klassenvererbung und Standardwerten interagieren soll; abgeschlossen ist die Arbeit, wenn die Syntax und ihr Verhalten bei der Typprüfung spezifiziert und durch geeignete Compiler-Tests abgedeckt sind.

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
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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