microsoft / microsoft/TypeScript

Could there be a `This` type like the `Self` type in rust ?

Ouverte
#44,055 7 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

🔍 Search Terms

This, Self, typeof this

✅ 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

For example, let's declare an interface Cloneable, instances of classes implementing Cloneable should be able to clone themselves:

interface Cloneable {
    clone(): This
}

The return type of function clone() of interface Cloneable should be the actual type of this, or typeof this in the shown code. However typeof this does not work. It will raise TS2304 Cannot find name 'this', and TS4055 Return type of public method from exported class has or is using private name 'this'.

clone(): Cloneable is not the desired way for this interface, or the below code would pass type check:

class A implements Cloneable {
    clone() { return new B() }
}
class B implements Cloneable {
    clone() { return new A() }
}

Besides, clone() is not returning this. So clone(): this is not the right way to go. It won't allow this to pass:

class A implements Cloneable {
    clone() { return new A(this)  }
}

Generics could be a workaround, but it can be annoying for complex cases, and still can be a problem:

class A implements Cloneable<B> {
    clone() { return new B() }
}

It does not make any sense to have sth like class A implements Cloneable<B>, but generics allows it.

Allowing typeof this, or This, could be a very useful in that case.

This feature could also help in some other cases, like interface Addable:

interface Addable {
    add(that: This): This
}

The argument that of function add must be of the same type of this, so it cannot be declared as add(that: Addable): Addable. It also cannot be add(that: this): this.
It makes sense to use generics,

interface Addable<That> {
    add(that: That): That
}

But still, it is not a very good way.

📃 Motivating Example

💻 Use Cases

As discussed above, it is mainly used by interfaces and abstract classes, like interface Cloneable, interface Addable, or maybe interface Serializable and interface Serializer.
The current approaches cannot provide the correct behavior, which is discussed above.
I am using generics, but it is incorrect indeed.

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

L’issue ne nomme aucun fichier ni test du dépôt. Commencez par examiner le comportement existant du type this et les objectifs de conception de TypeScript référencés dans l’issue, puis comparez les exemples Cloneable et Addable avec leurs solutions de contournement génériques ; la tâche serait considérée comme terminée une fois la sémantique établie et le plan d’implémentation du type proposé définis.

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é
Clairement spécifiée
Accessibilité débutants
30/100

Recevez les nouvelles issues par e-mail

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