microsoft / microsoft/TypeScript

Explain why private and protected members in a class affect their compatibility

Ouverte
#18,499 29 commentaires 39 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

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

Description

TypeScript Version: up to 2.5.2 at least

In the doc a short paragraph explains that private and protected members in a class affect their compatibility.

I have been searching for a while in the design goals, on SO etc... but could not find a decent explanation of the rationale. Could we:

  • either document the rationale (in the paragraph above?
  • or consider evolving the language?

Note: I found one rationale close to what I am looking for, but you could imagine that the language "reserves" the private names but does not compel to use them.

Code
This behavior is especially an issue in unit testing where you want to mock a class, disregarding its internals...

class MyClass {
    pubfield = 0;
    private privfield1 = 0;
    constructor(private privfield2: number) {}
    private method(): void {}
}

class MyClassMock implements MyClass {
    pubfield: number;
    // *** compile errors on missing properties ***
}

Expected behavior:
I would like to only care about the public contract that a class-under-test can possibly use from my mock.

Actual behavior:
I cannot limit my mock to the public fields.

The ugly workaround I found is the following:

class MyClass {
    pubfield = 0;
    private privfield1? = 0;
    private privfield2? = 0;
    // do not use the shortcut notation because the constructor param is not optional!
    constructor(privfield2: number) {
        this.privfield2 = privfield2;
    }
    private method?(): void {}
}

class MyClassMock implements MyClass {
    pubfield: number;
}

What I do not like is that I have to modify the semantics of my class to be able to mock it and that I am scared that a future overzealous refactoring may use the shortcut notation for fields declaration in the constructor... making the constructor param optional (!)

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 la section Private and Protected Members in Classes de pages/Type Compatibility.md et lisez le commentaire lié de l’issue concernant la justification existante. Comparez cette explication avec l’exemple mock et les alternatives demandées. C’est terminé lorsque la justification est documentée clairement ou que la question de conception du langage fait l’objet d’une décision consignée.

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

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Documentation
Difficulté
4/5
Temps estimé
3-5 jours
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.