Skip to content

Suggestion: Add readonly references to objects & mark readonlyThis methods (pure functions) #42758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
5 tasks done
emilioplatzer opened this issue Feb 11, 2021 · 0 comments
Open
5 tasks done
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@emilioplatzer
Copy link

Suggestion

πŸ” Search Terms

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code.
    • It can or cannot be. It depends on wich name we choose
  • 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

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

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. 

πŸ’» Use Cases

Actually I'am ussing Readonly aproach, but it not contemplates all cases.

play example

I am trying with Object.seal and Proxyies .

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Feb 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants