This project is read-only.

Read-only members

Topics: General, Language Specification
Dec 20, 2012 at 8: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 7: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 2:44 PM
Edited May 10, 2013 at 2: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 10: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.