This project is read-only.

Check use of properties that have ony a getter or only a setter

Topics: General
Oct 7, 2012 at 9:59 AM

Getter and Setter are supported, but currently the typechecking only handles them as normal properties. It would be nice to check the use of properties that have only a getter or only a setter:

class Test {
    private counter: number;
    get name(): string { return 'My Test' }
    set hiddenCounter(v: number) { counter = v; }
var test = new Test; = 'Error';
var error = test.hiddenCounter;

These two errors should be recognized by type checking and in the code completion could be an icon G (only getter) and S (only setter) for these properties.

And it should be possible to declare such a class:

declare class Test {
	private counter: number;
	get name(): string;
	set hiddenCounter(v: number);
Currently I get the error: "Property accessors may not be declared in ambient classes".

Oct 10, 2012 at 7:55 PM

Hi Andy,

The first example you give actually matches the semantics of JavaScript, which is something we want to do.  Though, we get requests to address this quite often, and are looking into introducing an error for it in the future.

For ambient classes, the reason why we don't allow getters and setters is because from a the type system's point of view, it's really just a normal property.  (For typing purposes, the fact that a property may be backed by a getter or setter isn't relevant.)  So technically you'd want to use "name: string" or "hiddenCounter: number".


- Joe

Oct 11, 2012 at 1:10 AM

Hi Joe,

thank you for your reply.

In strict mode JS throws an error if I try to "Writing to a read-only property" e.g. if it has only a getter (

So I hope that you will add more infos to a property for the type system in the future than only the type, to get errors for read-only properties at compile time.

Oct 11, 2012 at 10:23 AM

Hi Andy,

I agree with you that is would be nice to add additional info to properties. Support for property accessors in ambient declarations would be indeed be great idea.

For example when you look at the DOM API, there are so many properties that are officially defined as read-only. To assist developers to not make mistakes and try to assign a value to these properties would be a huge benefit. Right now there is no way of knowing which properties can be set and which are read-only.

So I also hope that the TypeScript team would at least consider to put this on the roadmap (to be fair, i guess this ain't a small change).