GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Suggestion: Use the keyword "readonly" as an accessor for classes · Issue #10064 · microsoft/TypeScript

Suggestion: Use the keyword "readonly" as an accessor for classes #10064

New issue
Copy link
New issue
Copy link
Closed
Closed
Suggestion: Use the keyword "readonly" as an accessor for classes#10064
Copy link
Labels
DuplicateAn existing issue was already createdAn existing issue was already created

Description

@paleo
Thomas Mur (paleo)
opened on Aug 1, 2016
Issue body actions

Use case: we need a class Counter which provides a property count that is read from outside the class and written from inside the class.

In JavaScript, if we want to use a class instead of a closure, we have to make a public property:

/**
 * Read-only access to the property "count", please
 */
class Counter {
    constructor() {
        this.count = 0
    }
    inc() {
        ++this.count
    }
}
let counter = new Counter()
counter.inc()
console.log(counter.count) // 1
counter.count = 10 // No error but it doesn't respect the JSDoc

In TypeScript, we could proceed in the same way. But it is tempting to use an accessor private:

class Counter {
    private count = 0
    public inc() {
        ++this.count
    }
    public getCount() {
        return this.count
    }
}
let counter = new Counter()
counter.inc()
console.log(counter.getCount()) // 1
counter.count = 10 // Error: Property 'count' is private and only accessible within class 'Counter'

However, it is verbose, less readable, less performant .

I suggest the following syntax:

class Counter {
    readonly count = 0
    public inc() {
        ++this.count
    }
}
let counter = new Counter()
counter.inc()
console.log(counter.count) // 1
counter.count = 10 // Error: Property 'count' is readonly and can be only mutated within class 'Counter'

It is the same solution as in JavaScript, but with a better control to access (and less JSDoc).

Activity

  1. RyanCavanaugh commented on Aug 1, 2016

    @RyanCavanaugh
    Ryan Cavanaugh (RyanCavanaugh)
    on Aug 1, 2016
    Member
    More actions

    readonly has already been implemented for class properties, though it has different semantics from what you're proposin. See #12 and #6532

  2. Ryan Cavanaugh (RyanCavanaugh)
    added
    DuplicateAn existing issue was already createdAn existing issue was already created
    on Aug 1, 2016
  3. Mohamed Hegazy (mhegazy)
    closed this as completedon Sep 20, 2016
  4. microsoft
    locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.