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<T> prevents private members from being accessed #50668

Closed
shrinktofit opened this issue Sep 7, 2022 · 4 comments
Closed

Readonly<T> prevents private members from being accessed #50668

shrinktofit opened this issue Sep 7, 2022 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@shrinktofit
Copy link

Bug Report

🔎 Search Terms

private, readonly, remove, class, exclude

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

Playground link with relevant code

💻 Code

class Foo {
    public bar = 0;
    private _bar = 0;
    #bar = 0;

    public baz(that: Foo, readonlyThat: Readonly<Foo>) {
        this.bar; // OK
        this._bar; // OK
        this.#bar; // OK

        that.bar; // OK
        that._bar; // OK
        that.#bar; // OK

        readonlyThat.bar; // OK
        readonlyThat._bar; // Error
        readonlyThat.#bar; // Error
    }
}

🙁 Actual behavior

Can not access private members from Readonly<T> version class, even within class body.

🙂 Expected behavior

Within Foo.baz(), all fields of objects with type Readonly<Foo> should be accessible.

@MartinJohns
Copy link
Contributor

MartinJohns commented Sep 7, 2022

This is working as intended. Mapped types like Readonly<T> use the keyof operator, and the keyof operator intentionally does not include non-public members.

Duplicate of #16441.

@shrinktofit
Copy link
Author

shrinktofit commented Sep 7, 2022

This is working as intended. Mapped types like Readonly use the keyof operator, and the keyof operator intentionally does not include non-public members.

Well. Is there any alternative way to implement "readonly class object"?

@MartinJohns
Copy link
Contributor

MartinJohns commented Sep 7, 2022

No. You'd need something like #35416 for this.

@jakebailey jakebailey added the Duplicate An existing issue was already created label Sep 7, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants