microsoft / microsoft/TypeScript
Support forwarding generic type in extending class
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la sintassi infer proposta e gli esempi Parent, Child, System, CorpSystem e SectionSystem nell’issue. Determina come il forwarding generico debba interagire con l’ereditarietà delle classi e i valori predefiniti; il lavoro è completo quando la sintassi e il suo comportamento nel controllo dei tipi sono specificati e coperti da test appropriati del compilatore.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100