Repository: TypeScript
Issue: Suggestion: Add readonly references to objects & mark readonlyThis methods (pure functions)
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:
-->

   * readonly method
   * readonly this
   * sealed class
   * sealed this
   * readonly reference #17181 (this issue is simmilar but more restrictive) 

## ✅ 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. 
   * It can or cannot be. It depends on wich name we choose
* [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 -->

Have a way to check readonly object references (including parameters, and *this* parameter). That allows to check than a function does not modifys an object. 

## 📃 Motivating Example

```ts

class Figure{
    constructor (public name: string){}
    checkName(){
        this.name += ' !'
    }
    readonlyMethod logAccess(){
        console.log('one instance of Figure was accesed');
    }
    readonlyMethod safeCheck(){
       // @ts-expect-error
        this.name += ' x'  // this is readonly in a readonlyMethod context
    }
}

function showRO(figure: readonlyReference Figure){
    console.log(figure.name);
    // @ts-expect-error
    figure.name+=" x";  // if figure is a readonly reference you can use it to modify the object
    // @ts-expect-error
    figure.checkName();  // if figure is a readonly reference you can use it to modify the object calling a method
    figure.logAccess();  // ok logAccess is a readonly method
}

var x = new Figure('triangle');

showRO(x);

var z: readonlyReference Figure = x; // ok, you can assign a writeable object to a readonlyReference

// @ts-expect-error
var y: Figure = z ;  // you cannot do the oposite. 

```


<!--
  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?
-->

Actually I'am ussing Readonly<ClassName> aproach, but it not contemplates all cases. 

[play example](https://www.typescriptlang.org/play?#code/MYGwhgzhAEBiCWBzArgJwKYG8CwAoaB0wA9gHYQAuqywFxq0AFAA7IBGI8w0pYAtugBc0SqnilEASkwBfPISIALdMADWAOX7pG0+QsIVF8CADpeA6AGoAvNADk0AB529BObnd4AZslK14ZCKKxADuAEoA8oxeSGhCYehgACZkIACeADwIKBgAfLr4hCTkxCDoJiDEiNGxGGZakgDcrtAA9K3QAAIUEAC06I7MKhT9qKj0LTE55eboNgBETvPNhQTtXT39g8Oj46iTteXAymqaAjornrgAbmAMjtC2pOghcIeMdlTwYBJldk14PAQYLhKKOAG4PBAA)

I am trying with Object.seal and Proxyies .