microsoft / microsoft/TypeScript

Access base class protected methods in static block

Ouverte
#62,738 6 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Domain: classes Help Wanted
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

🔍 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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par l’exemple TypeScript Playground lié et par la discussion de l’issue sur les blocs statiques, les méthodes protégées et les remplacements de prototypes. Suivez la manière dont l’accès proposé au prototype de base serait vérifié par le système de types. Le travail est terminé lorsque le comportement est clairement défini et que les cas pertinents de vérification des types sont couverts.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript, typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.