-
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
SUGGESTIONS: getters/setters in interface #11878
Comments
What's the difference between this and |
There is runtime difference. getter has |
@RyanCavanaugh 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 |
@dungdm93 but that makes no difference from an interface perspective. |
@kitsonk OK. maybe my example make confused. |
Using |
Why is it confusing? The interface describes the types of an interface, not the implementation, the the ability to assign values. You are suggesting that the interface should concern itself with implementation details? That is unsound. For example, from an interface perspective what is the difference between a getter only property that returns a string and a non-writable property that has a value of string? Why would the interface be concerned about that distinction? |
I'm not mean the interface should care about implementation details itself.
So my suggestion is allow abstract getter act the same as |
In regards to Again, as far as read only, what is the difference between: interface Foo {
readonly foo: string;
}
class FooA implements Foo {
readonly foo: string;
constructor() {
Object.defineProperty(this, 'foo', {
value: 'bar',
writable: false,
enumerable: true,
configurable: true
});
}
}
class FooB implements Foo {
get foo(): string {
return 'bar';
}
} Answer is nothing. There is more than one way of implementing a read only property in JavaScript, therefore the interface shouldn't be concerned about implementation. |
Just to say this explicitly, because there seems to be some confusion: |
@RyanCavanaugh So sad. I totally known the difference between abstract class Abstract {
abstract set foo(value: string);
abstract get bar(): string;
}
class Concrete implements Abstract { // Make class be interface => It's work
private f: string;
private b: string;
set foo(value: string) {
this.f = value;
}
get bar(): string {
return this.b;
}
} Why we can't declare like: interface Abstract {
set foo(value: string);
get bar(): string;
} |
An interface represents a contract, a shape, not how it is implemented. In JS there are two ways to implement properties, either as property declarations, or as accessors. From the user of the object perspective it does not matter. Consider this: var i1 = {
p: 1
};
var i2 = {
get p() { return 1; }
set p(v) { }
}; From the user perspective, both Thus the type is: interface I {
p: number;
} hope this makes it clear. |
In TypeScript, we define abstract property in interface like:
But there are no way to define only setter.
And currently we can use
readonly
property to defne abstract getter like:Even that, I realize that
readonly
have many drawbacks as:ambiguous meaning
When using
readonly
keyword, most people expect an un-reassignable property (as they are in class). However, in interface it declare an abstract getter.In bellow example, some guys implement
readonly
property as computed property.missing implement
Because interface make no distinction between regular properties and accessor properties. So compiler can't detect errors if child classes don't implement correctly.
Suggestion
I'd like to suggest that abstract getter/setter (accessors) is allowed in interface.
The text was updated successfully, but these errors were encountered: