Repository: TypeScript
Issue: Need support this keyword in class member's return object type
Labels: Suggestion,Awaiting More Feedback

# Suggestion

<!--
  Please fill in each section completely. Thank you!
-->

## 🔍 Search Terms

<!--
  💡 Did you know? TypeScript has over 2,000 open suggestions!
  🔎 Please search thoroughly before logging new feature requests as most common ideas already have a proposal in progress.
  The "Common Feature Requests" section of the FAQ lists many popular requests: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-feature-requests

  Replace the text below:
-->

inaccessible this 
thistype

## ✅ 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

<!-- A summary of what you'd like to see added or changed -->

Need some mechanism to specify this reference in interface, so we can generate d.ts with complex class member types
```typescript
class Test{
  Value: { a: number }
  get value(): this['Value'] {
    return {a: 1}
  }
  get object(): ThisType<this> & { foo: this['Value'] } {
    return { foo: this.value }
  }
}
```

## 📃 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?
-->
`this` reference is very usefull in class, It's more flexibly way compare to generic, but currently It's not fully usable in declaration generation.

## 💻 Use Cases
[playground](https://www.typescriptlang.org/play?#code/MYGwhgzhAEAqCmEAuBvAsAKGt6ATeoYATvNAGpggCu8AXNCtGPQHZUC2ARvEdAL6Yc0APTDoAewDW0AO4ALeCzwBLEsCQgAntCoRSSOcoiCcAc3hJoAN0o0AFAEp6BowG0A5BWrx3AXQYmQtgkSFRESijM0ACMAlg4cUKi0ACSSO4wUrLKBngE4ERgSMriLPQAZpR6gdDmliziSADyktGO6PFB0CFhETVd2FbOhhAAdDbe-Qk1iTjJDZZZ8oo6etAuMMpKW0g8lcDwADSp6TAQyuzKBdm5AAYLLdG3NXXQD5IATI70jEPrIx4vDQ-PwOgNuhZegFOuDrMMjONbPApthZqiaskAHLweC4aAQcTsUhE4ByMAsIzsdbifEABwIynK2g2EPKPEUB2gWy5LF2RH2R2p0EUYE4IFIo1woyQmSoSFpctGLwsb0aLQAzN8GHD-m5PEiQXxoAAyOAjWCaekAHg2AD5oeCeuEHbDBvCxhMaCj+DNMHFMMkAOpybS0khs3gbDwAD1jIKQNPMLB4ymAAH4ThloOxxCRoOVxdHlGLkRhCFBoABlKicYXR3YsXAwBDIME4fCEPNAujaqJsLg8Y6cejIIhbUw+jBxZMyKs1xyI7yjTiYGdzzgL97RcbLoA)
```typescript
class Test{
    declare Value: { a: number }
    // ok when directly use this
    get value(): this['Value'] {
        return {a: 1}
    }
    // It's ok with declaration: false
    get notOk1(){
        return {
            v: this.value
        }
    }
    // not ok when use this in interface, It's similar with `notOk1`
    get notOk2(): { v: this['Value'] }{
        return {
            v: this.value
        }
    }
    // Need some mechanism to specify this reference in interface, to enable .d.ts output.
    get notOk3(): { v: this['Value'] } & ThisType<this> {
        return {
            v: this.value
        }
    }
}

// Why prefer this['xxx'] to generic? It's more flexible
class Sub extends Test{
    declare Value: { a: number, b: string }
}
new Sub().value.b
new Sub().notOk1.v.b
```
<!--
  What do you want to use this for?
  What shortcomings exist with current approaches?
  What workarounds are you using in the meantime?
-->