77

Closed

Get/set accessors in interfaces

description

The request is really for the ability to describe read-only (or write-only) properties in an interface.
Closed Jul 28, 2014 at 11:17 PM by jonturner
As part of our move to GitHub, we're closing our CodePlex suggestions and asking that people move them to the GitHub issue tracker for further discussion. Some feature requests may already be active on GitHub, so please make sure to look for an existing issue before filing a new one.

You can find our GitHub issue tracker here:
https://github.com/microsoft/typeScript/issues

comments

andy_dots wrote Oct 16, 2012 at 5:26 PM

In strict mode JS throws an error if it is tried to "Writing to a read-only property" e.g. if it has only a getter (http://msdn.microsoft.com/en-US/library/br230269%28v=vs.94%29.aspx).

So I hope that TypeScript 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.

LukeH wrote Jan 19, 2013 at 12:52 AM

As suggested in the description, the likely way this would be done in TypeScript is to support a notion of "readonly" on interface members. Get-only accessors in classes would implicitly define a readonly property, but other patterns could also indicate that at compile-time TypeScript should error on attempts to modify a property, even if it is not strictly protected at runtime.

Bartvds wrote Apr 24, 2013 at 10:30 AM

I would like to be able to describe getters and setters in interfaces, maybe similar how ActionScript 3.0 handled this.
interface IVisible {
    get visible():bool
    set visible(value:bool)
}
read-only:
interface IVisible {
    get visible():bool
}
write-only:
interface IVisible {
    get visible():bool
}

SelFC wrote Nov 26, 2013 at 8:35 AM

There is an error in write-only interface. It should be: set visible(visible:bool);

EeliK wrote Jun 5, 2014 at 8:39 AM

The most simple syntax would be
interface IFace {
    get set visible: bool
}
For at least readable:
interface IFace {
    get visible: bool
}
For at least writable:
interface IFace {
    set visible: bool
}
Implementation would decide if it provides more than promised. For example
interface IFace {
    set visible: bool
}
could be implemented either as
public visible: bool
or
set visible(visible: bool): number {
        this.visible = visible;
    }

EeliK wrote Jun 5, 2014 at 8:41 AM

Sorry, I didn't first understand that some people want set a property as read-only (the topic said otherwise). But the same syntax [get] [set] prop could be used.

EeliK wrote Jun 5, 2014 at 9:07 AM

The more I think about this the more I feel that restrictions don't belong to interfaces. Interface promises you can do something with an implementation, not that you can NOT do something. It would be same as adding a new imaginary keyword "never":

never DoSomething();

which would promise that implementations couldn't add method "DoSomething".

Bartvds wrote Jun 5, 2014 at 11:30 AM

Interfaces are all about restrictions and surface. It describes the contract, so if you have classes with getters and want to unify their shape in an interface then a getter annotation is the right thing to do. Same with setters.

I like Eelik's long example 3 comments op. I feel nothing for never, that is just weird.