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

A better way to define readonly properties ? #1737

Closed
NN--- opened this issue Jan 20, 2015 · 4 comments
Closed

A better way to define readonly properties ? #1737

NN--- opened this issue Jan 20, 2015 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@NN---
Copy link

NN--- commented Jan 20, 2015

Current solution involves a lot of boilerplate code:

class A {
 constructor(a: number) {
  this._a = a;
 }

 private _a: number;
 get a(): number { return _a; }

It would be nice to have something simpler.
Now you can define properties from the constructor, why not define getter there as well ?

class A {
 constructor(get a: number) {
 }
@danquirk
Copy link
Member

You can use parameter properties today for similar shorthand:

class A {
    constructor(public x: number, private y: string) {}
}

var a:A;
var r = a.x; // number
var r2 = a.y; // error

See #12 for read only issues.

@danquirk danquirk added the Duplicate An existing issue was already created label Jan 20, 2015
@NN---
Copy link
Author

NN--- commented Jan 20, 2015

I know about shorthand.
My intention is shorthand for readonly from constructor.
I didn't see this proposal in the #12 .

@fabm22
Copy link

fabm22 commented Feb 12, 2016

@danquirk You sure meant to write a y in second parameter of your constructor right ??

@danquirk
Copy link
Member

Correct. Fixed. You'll probably be interested in #6532

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants