microsoft / microsoft/TypeScript

Support for both public and protected constructor overload signatures

Ouverte
#43,616 8 commentaires 3 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

Suggestion

TypeScript already supports abstract class, which cannot be instantiated (of course, I know they can — it's just the compiler that disallow that). Non–abstract classes, on the other hand, can be instantiated using any constructor overload they provide.
There are cases, however, when you may want to allow creation of a class using only one constructor overload and provide another one for subclasses to use. The latter is what I would call “protected constructor,” which could be defined with the addition of the modifier protected. However, it looks like TypeScript currently doesn't support overloads with different access modifiers; I wonder why, as signature matching is merely a static check and doesn't change the way the method is actually invoked. If different overload signatures were allowed to support different access modifiers, the scenario with a protected constructor and a public one I described would be permitted.

🔍 Search Terms

protected constructor
abstract constructor

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Just allow having different access modifiers for different overload signatures. There's no need I can see to disallow that and it wouldn't be difficult at all to implement.

📃 Motivating Example

Suppose I want to use the composition pattern and provide default sub–instances in the public constructor but still allow subclasses to pass their own sub–instances, as follows:

class Controller {}

class View {

    constructor();

    protected constructor(controller: Controller);

    constructor(readonly controller: Controller = new Controller()) {}
}

class CustomController extends Controller {}

class CustomView extends View {

    constructor() {
        super(new CustomController());
    }
}

💻 Use Cases

Additionally to the “protected constructor” use case I described above, since such a feature requires, as previously mentioned, allowing different access modifiers across overload signatures, this change would also permit a lot of other things. I suppose you all can understand that having protected signatures for a subclass and exposing only public signatures for other sites may turn useful.

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 examiner la gestion par TypeScript des signatures de surcharge de constructeurs et des modificateurs d’accès. Validez l’exemple motivant avec une surcharge de constructeur publique et une surcharge protégée pour les sous-classes. Le travail est terminé lorsque l’exemple passe la vérification de type, tandis que les appels utilisant la signature protégée restent indisponibles pour le code non apparenté.

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

Évaluation

Stack technique
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.