Read-only members

Topics: General, Language Specification
Dec 20, 2012 at 7:32 PM

Are there are near-future plans to add readonly member support to TypeScript? i.e.

class Point
    public static readonly Zero = new Point(0, 0);

Point.Zero = new Point(1, 1); // Won't compile

At the moment, I'm achieving this with a private member and a property getter.

Thanks, and great work on TypeScript. I only discovered it this week and I'm already in the middle porting my current project over to it. I hope to see some new developments soon!

Dec 28, 2012 at 6:28 PM

Readonly is on our backlog for discussion, but no concrete plans currently. If you're targeting ES5, then using a getter is the best approach currently.

Thanks for the kind words and for using TypeScript!

May 10, 2013 at 1:44 PM
Edited May 10, 2013 at 1:46 PM
As part of the discussion, will "readonly" convert to:
Object.defineProperty(Point, "Zero", { value: new Point(0, 0), writable: false });
Getters are great, but not as performant.

It would also be nice to be able to create a constructor as follows:
constructor(public readonly Point: Point) {
Sep 12, 2013 at 9:00 PM
After days of trying to find an alternative solution, I ended up changing all my interface to abstract classes and define read onlyproperty that way.