Skip to content

Suggestion: readonly constructor parameters #7590

Closed

Description

@jahewson

I've been using the new readonly properties feature (#6532) and have found it to be a very productive alternative to using getters or getWhatever() functions, or as is often the case, simply doing nothing to enforce read-only properties. However I'm really yearning for the ability to mark constructor parameters as readonly.

In 1.8 a common pattern for me is to write classes where all properties are public and readonly like so:

export class Foo {
  constructor(public bar: number,
              public baz: number) {}
}

But now with readonly I'm missing out unless I rewrite the above code to:

export class Foo {
  public readonly bar: number;
  public readonly baz: number;

  constructor(bar: number, baz: number) {
    this.bar = bar;
    this.baz = baz;
  }
}

Which is pretty cumbersome. What I'd really like to be able to write is:

export class Foo {
  constructor(public readonly bar: number,
              public readonly baz: number) {}
}

Right now I'm worried about hitting 2.0 and having to rewrite all my constructors just to take advantage of readonly. Because it's an advantage I definitely want to have.

Activity

  1. DanielRosenwasser commented on Mar 19, 2016

    @DanielRosenwasser
    Member

    So would a readonly parameter be treated as a const within the body of its constructor?

  2. jahewson commented on Mar 21, 2016

    @jahewson
    Author

    No, it would simply be a shorthand for creating properties in the manner of constructor(public foo) { ... }, only we'd get a readonly property.

  3. added and removed
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    on Mar 24, 2016
  4. leonyu commented on Apr 4, 2016

    @leonyu

    This can probably be implemented using a decorator on the function doing a defensive copy on the arguments. However, that would be a runtime check.

  5. jahewson commented on Apr 5, 2016

    @jahewson
    Author

    Leon Yu (@leonyu) Copying the arguments is not what I'm after. I'm asking for exactly the same semantics as readonly. No copying, no runtime checks. Just a shorthand for creating a readonly property, in the same way that adding public to a parameter is a shorthand way to create a public property.

  6. nbransby commented on May 4, 2016

    @nbransby

    +1

  7. RyanCavanaugh commented on May 9, 2016

    @RyanCavanaugh
    Member

    Makes a lot of sense. We'll include this along with the first release with readonly

  8. jahewson commented on May 10, 2016

    @jahewson
    Author

    Awesome, thanks!

  9. ghost added a commit that references this issue on May 11, 2016
    fb7df96
  10. ghost added a commit that references this issue on May 11, 2016
    ad2634e
  11. ghost closed this as completedin #8555on May 12, 2016
  12. ghost added a commit that references this issue on May 12, 2016
    7806de0
  13. aluanhaddad commented on May 23, 2016

    @aluanhaddad
    Contributor

    Really glad this is implemented. In addition to the increased expressiveness, this allows a fair amount of code currently using accessibility modifiers for the sake of brevity to be written with clearer intent.

  14. 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

    CommittedThe team has roadmapped this issueFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions