I noticed that typescript 2.0 will support readonly class properties, but it isn't available yet. The reason for this post is that I want to be smart about how I write my code today so that I can transition it with minimal pain later.
I want to use a class with readonly properties this way:
let widget = new Widget(110, 220); // width, height
//...
widget.width(200); // to modify the width, I use a setter with validation
widget.width = 300; // note: I want this to be a compile time error
//...
let w = widget.width; // and later I want to get/draw the current width
...but I can't get this to work due to fn and property having the same name - so I'll switch fn name to setWidth. I'd rather not prefix all my readonly properties with _ because in the past I've found that the pain is more than just the hassle of keying the extra character. And because of TS's type checking, I don't need the visual reminder that the property is readonly (or private).
Q1: Will the above work in typescript 2 if width is a readonly property of the widget class as below?
export class widget {
readonly width:number; // doesn't work in TS 1.8
setWidth(w:number) {
this.width = w;
}
}
In the near term, I can either make the property public so I can access directly, with the 'risk' that I might set the property directly - or I can have private properties and write getters and setters.
Q2: If I do the latter, how much slower is the getter than direct property reference? Some of my properties will be called very frequently while dragging, so speed is important.
widget.getWidth();
// vs
widget.width;
UPDATE
I recently found this posted answer and realized that TS already supports most of what I was trying to do:
export class Widget {
private _width:number;
set width(w) {if (w >= 0) this._width = w}
get width() {return this._width}
}
Usage syntax is same as for public property with no accessors, which is really nifty:
let widget = new Widget(...);
let existingWidth = widget.width;
widget.width = newWidth;
I'm hoping that TS2's readonly qualifier will give me direct read access to (private) properties (not through an accessor function, so faster), and none of my usage code will need to be changed. Anyone know?