microsoft / microsoft/TypeScript

Allow non-inherited static class members.

Ouverte
#31,493 1 commentaire 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.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

Search Terms

inheritance inherit static class extendStatics

Suggestion

Provide a way to mark static class members as non-inherited. This is particularly meaningful for factory methods.

Use Cases

Static factory methods are often useful because they can be used for async construction of an object. They also can be useful for providing alternative constructor implementations (effectively) since TypeScript doesn't support constructor overloads (without a bunch of conditional ugliness in the constructor). Unfortunately, while the constructor for a subclass isn't inherited by the superclass, all static members are which means you cannot have a factory method like create or from in both the subclass and the superclass. One of them gets the name, the other doesn't.

Looking at the emitted code, there is explicit code for emitting static class extension. It would be nice if there were a way to tell the compiler to not emit static extension for a particular static method.

Examples

class Point1 {
    public constructor(private readonly x: number) { }
}
class Point2 extends Point1 {
    public constructor(x: number, private readonly y: number) { super(x) }
    public static from = (point1: Point1, y) => new Point2(point1.x, y)
}
class Point3 extends Point2 {
    public constructor(x: number, y: number, private readonly z: number) { super(x, y) }
    public static from = (point2: Point2, z) => new Point3(point2.x, point2.y, z)
}

The above code currently fails to compile. It would be nice if I could somehow annotate the from method on Point2 such that it was not inherited. Currently, if I rename them to from1 and from2 then a user calling Point3.from1(...) will get back a Point2 instead of the expected Point3. When I am authoring a Point2 factory method, I want to tell the compiler, "It doesn't make sense to call this static method from the context of a derived class".

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.

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 logique du compilateur qui émet l’extension de classes statiques ainsi que le comportement de la vérification des types pour les membres statiques hérités. Utilisez les exemples Point1, Point2 et Point3 comme cas de reproduction ; la tâche sera considérée comme terminée lorsqu’une annotation ou une conception du langage aura été convenue, que la vérification des types sera correcte et que des tests couvriront le comportement d’héritage qui en résulte.

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
25/100

Recevez les nouvelles issues par e-mail

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