-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No way to declare setter/getter only in interfaces #10196
Comments
Use the readonly keyword instead of the get keyword: export interface ITimer {
readonly isRunning: boolean;
start(): void;
stop(): void;
reset(): void;
} |
Interfaces make no distinction between regular properties and accessor properties--it's an implementation detail left up to the implementer of the interface. As suggested above, use the |
Thanks.. I guess I'll have to get used to write "readonly" :) |
@ahejlsberg Another bad case is implement interface IPerson {
readonly fullName: string;
}
class Person {
public firstName: string;
public lastName: string;
constructor(firstName: string, lastName: string) {
this.firstName = firstName;
this.lastName = lastName;
}
public get fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
let p: Person = new Person("Bruce", "Wayne");
alert(p.fullName); // Bruce Wayne
p.firstName = "Clark";
p.lastName = "Kent";
alert(p.fullName); // Clark Kent From my aspect, allow getter/setter declared in interface make more sense: interface IPerson {
get fullName(): string;
} |
@dungdm93 But it is not reassignable, so it makes sense? |
I'm trying to get the same working but i get an error:
What i try to do:
|
@andre-dw you declared |
export interface ITimer {
get isRunning():boolean;
start():void;
stop():void;
reset():void;
}
Why is this not working? This is the most logical thing anyone would expect. The value of the isRunning property should be changed, only inside the class instances. Otherwise anyone could change it outside, which is not good.
The text was updated successfully, but these errors were encountered: