microsoft / microsoft/TypeScript

Access base class protected methods in static block

Aperta
#62,738 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: classes Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

🔍 Search Terms
  • Dynamic method override
  • Static block super
  • Static block prototype
✅ Viability Checklist
⭐ Suggestion

Hey there, I have a use case where I want to declare/override a protected method using it's prototype.

With the addition of static blocks I can now override the method itself, however I can't call the same method of the base class using super. This makes it impossible to create dynamic overrides of a prototype method atm.

My suggestion would be to allow the usage of the base type prototypes in a static block of the class as well.
This would make it a lot easier to dynamically override/declare a method without using instance fields.

📃 Motivating Example

TypeScript now supports dynamic method overrides using static initialization blocks instead of runtime checks.

💻 Use Cases

1. What do you want to use this for?
Dynamic method declarations / overrides
Dynamic class creation using an options object.

2. What shortcomings exist with current approaches?
Currently the only way I see to do this is either:

  • Use as any/ which destroys type information
  • Use as DerivedType which allows the call with type information
  • Always override the method and call the additional method in an if branch which usually requires an instance field
  • Declare the method on an instance field instead of the prototype which uses more memory
  • Use @ts-ignore because you know what you are doing

3. What workarounds are you using in the meantime?

  • I use (Base.prototype as DerivedType).myMethod which imho is the best solution atm

Example:
https://www.typescriptlang.org/play/?ssl=25&ssc=2&pln=1&pc=1#code/MYGwhgzhAECyCeBhcVoG8BQ1oAcBOA9gC4CmwpAJtALbywlEAWBFAFAJTpbbQD0v0AHTCIBaiWggCAcwCWwbgF8MyjKEgwEAERJ5ZANxIVkG6CQAepAHYVNSFBEzZ8xMpWgFDePRQm16TCwcXDx8AsKC0KLi0GAUFLJEsgRWYCCSMvLc2BAArji6gv4MzGzsShjcEERgScAhPEyyEIIuRMTwBUV0JSzQALzQAGa5VuTJVqxNEABccPA6eobGDpxOoWFCwlFiEnEJSSlpGXIK2Tx5BXjdAaXB-FH5utCjvup4RtBEEABMAGx-AAMlQ28xMUFahHaRE6JBuvQogmAaRAU0YzXYAG5NlZiLEQFIAO6fb4-AAsZL+II2rAQ4JabQ6BViMDAVng7HhgURyIJaIxmwAKuiYISCHgANYwABGuSI0GaLysEDAQxI52wtPsGkhxCZezsiwMRnpnOK3KRKP5EE4D2FirFkplcoVMFGKrV52U2GUiiAA

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con l'esempio collegato di TypeScript Playground e con la discussione dell'issue sui blocchi statici, sui metodi protetti e sulle sovrascritture dei prototipi. Traccia come verrebbe verificato dal sistema dei tipi l'accesso proposto al prototipo di base. Il lavoro è completato quando il comportamento è chiaramente definito e i casi rilevanti di verifica dei tipi sono coperti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, 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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.