question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[feature] class properties that are "readonly in public, writable in private" or other permutations

See original GitHub issue

Search Terms

typescript readonly on the outside writable on the inside

Suggestion

Some sort of syntax to describe readonly on the outside, writeable on the inside for a given property, so we can avoid making a getter just for this purpose (or using any of the convoluted options linked in that StackOverflow post).

Use Cases

To make this pattern easier to express.

Examples

instead of having to write

class Foo {
  private _foo = 123
  get foo() {
    return this._foo
  }

  changeIt() {
    this._foo = 456
  }
}

const f = new Foo
f.foo = 345 // ERROR

we would be able to write something shorter like

class Foo {
  publicread foo = 123

  changeIt() {
    this.foo = 456
  }
}

const f = new Foo
f.foo = 345 // ERROR

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:162
  • Comments:65 (3 by maintainers)

github_iconTop GitHub Comments

20reactions
yvbeekcommented, Jun 23, 2020

This is one of the features I miss so much in TypeScript.

I don’t really like the publicread privatewrite notation. Is there an other language that uses that notation?

Here are a few suggestions:

// Option 1: C# style
public name: string { get; private set; }

// Option 2: Swift style
private(set) name: string

// Option 3: Swift struct-style
public readonly name: string

mutating changeName(name: string) {
  this.name = name
}

// Option 4: New keyword
public frozen name1: string
public readonly name2: string

My choice would be the Swift style (option 1) or C# style (option 2). Option 3 is for if we can’t change the property definition.

17reactions
khokmcommented, May 17, 2021

Any progress on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript - A property that is read-only outside of the class, but ...
A second private property xValue can be declared. It would contain the value and be editable from inside. The public property x would...
Read more >
Feature Class properties—ArcGIS Pro | Documentation
For a feature class, the Describe dataType property returns a value of "FeatureClass". Properties. Property, Explanation, Data Type. featureType. (Read Only).
Read more >
explicit-member-accessibility - TypeScript ESLint
TypeScript allows placing explicit public , protected , and private accessibility modifiers in front of class members. The modifiers exist solely in the ......
Read more >
TypeScript: JavaScript With Syntax For Types.
TypeScript extends JavaScript by adding types to the language. TypeScript speeds up your development experience by catching errors and providing fixes ...
Read more >
Making a read-only attribute - Python Morsels
We can't assign to area because properties are read-only by default. We could try to make our length attribute into a property, by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found