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

Suggestion: readonly constructor parameters #7590

Closed
jahewson opened this issue Mar 19, 2016 · 8 comments
Closed

Suggestion: readonly constructor parameters #7590

jahewson opened this issue Mar 19, 2016 · 8 comments
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@jahewson
Copy link

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.

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Mar 19, 2016
@DanielRosenwasser
Copy link
Member

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

@jahewson
Copy link
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.

@mhegazy mhegazy added In Discussion Not yet reached consensus and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Mar 24, 2016
@leonyu
Copy link

leonyu commented Apr 4, 2016

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.

@jahewson
Copy link
Author

jahewson commented Apr 5, 2016

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

@nbransby
Copy link

nbransby commented May 4, 2016

+1

@RyanCavanaugh RyanCavanaugh added Committed The team has roadmapped this issue and removed In Discussion Not yet reached consensus labels May 9, 2016
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.0 milestone May 9, 2016
@RyanCavanaugh
Copy link
Member

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

@mhegazy mhegazy assigned ghost May 10, 2016
@jahewson
Copy link
Author

Awesome, thanks!

@ghost ghost closed this as completed in #8555 May 12, 2016
ghost pushed a commit that referenced this issue May 12, 2016
Fix #7590: Allow 'readonly' to be used in constructor parameters
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label May 12, 2016
@aluanhaddad
Copy link
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.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants