Repository: TypeScript
Issue: Could there be a `This` type like the `Self` type in rust ?
Labels: Suggestion,Awaiting More Feedback

# Suggestion

<!--
  Please fill in each section completely. Thank you!
-->

## 🔍 Search Terms

This, Self, typeof this

## ✅ Viability Checklist

<!--
   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.
   We always recommend reviewing the TypeScript design goals before investing time writing
   a proposal for ideas outside the scope of the project.
-->
My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).


## ⭐ Suggestion

For example, let's declare an interface `Cloneable`, instances of classes implementing `Cloneable` should be able to clone themselves:
```ts
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:
```ts
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:
```ts
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:
```ts
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`:
```ts
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,
```ts
interface Addable<That> {
    add(that: That): That
}
```
But still, it is not a very good way.

## 📃 Motivating Example

<!--
  If you were announcing this feature in a blog post, what's a short explanation that shows
  a developer why this feature improves the language?
-->

## 💻 Use Cases

<!--
  What do you want to use this for?
  What shortcomings exist with current approaches?
  What workarounds are you using in the meantime?
-->

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.