Skip to content
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

Readonly instances cannot have private members #23419

Closed
parzh opened this issue Apr 15, 2018 · 3 comments
Closed

Readonly instances cannot have private members #23419

parzh opened this issue Apr 15, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@parzh
Copy link

parzh commented Apr 15, 2018

TypeScript Version: 2.8.1

Search Terms: Readonly, freeze, Object.freeze, private

Code

There is a practice to freeze instances in the constructor, right before returning them. But, unfortunately current implementation of the Readonly interface makes it impossible to do so, if there are private instance members of any kind, be it fields, accessors or methods.

class Thingy {
  private doubleIt() {
    return this.value * 2;
  }

  constructor(public value: number) {
    return Object.freeze(this); // Property 'doubleIt' is missing in type 'Readonly<this>'
  }
}

This enforces developers to use workarounds and local usage conventions.

Expected behavior:
No compile errors.

Actual behavior:
Compile error: Property 'squareIt' is missing in type 'Readonly<this>'

Playground Link

Related Issues:
#22315

@ghost
Copy link

ghost commented Apr 16, 2018

I'll mark this as a duplicate of #10725 -- although we should probably not have used Readonly in that case since as you can see it doesn't work.

@ghost ghost added the Duplicate An existing issue was already created label Apr 16, 2018
@parzh
Copy link
Author

parzh commented Apr 16, 2018

I see the referenced issue as related to this one, rather than duplicate of it.

You're talking about Object.freeze() returning Readonly? No, I think, that's fine. The strange part is that readonly can be applied to properties only, – that's strange IMO. I mean, methods too in principle may or may not be read-only. Not sure about getters and setters though

@ghost
Copy link

ghost commented Apr 16, 2018

See #16441.

@parzh parzh closed this as completed Apr 16, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

0 participants