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

feature request: readonly only outside and writable inside #41643

Closed
5 tasks done
LongTengDao opened this issue Nov 23, 2020 · 1 comment
Closed
5 tasks done

feature request: readonly only outside and writable inside #41643

LongTengDao opened this issue Nov 23, 2020 · 1 comment

Comments

@LongTengDao
Copy link
Contributor

LongTengDao commented Nov 23, 2020

Search Terms

Suggestion

Sometime we need a value readonly from outside, but writable of cause inside. Usually we do it by double properties, but that's superfluous and waste of performance.

Use Cases

export default class Foo {

    private _value1;
    protected get value1 () { return this._value1; }

    private _value2;
    public get value2 () { return this._value2; }

    protected _value3;
    public get value3 () { return this._value3; }

};

We do all things above to make sure the value could only write from inside, otherwise, there will be a TS warn. If we want to save performance (ignore the superfluous problem being worse):

declare class _Foo {
    protected readonly value1;
    readonly value2;
    readonly value3;
}
const Foo = class {
    value1;
    value2;
    value3;
} as typeof _Foo;
type Foo = _Foo;
export { Foo as default };

Examples

One solution (everyone can suggest a better one of cause, I mainly point out the need):

export default class Foo {
    protected private value1;
    public private value2;
    public protected value3;
};

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • 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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@MartinJohns
Copy link
Contributor

Duplicate of #37487.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants