microsoft / microsoft/TypeScript

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

Aperta
#44,055 7 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

La issue non indica file o test del repository. Inizia esaminando il comportamento esistente del tipo this e gli obiettivi di progettazione di TypeScript a cui fa riferimento la issue, quindi confronta gli esempi Cloneable e Addable con i relativi workaround generici; per considerare il lavoro completato sarebbero necessarie semantica definita e un piano di implementazione per il tipo proposto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.