Iva*_*lev 8 getter setter properties interface typescript
我们在其中一个课程中有一个典型的getter,比方说
class Employee implements IEmployee {
private _fullName: string;
get fullName(): string {
return this._fullName;
}
}
Run Code Online (Sandbox Code Playgroud)
以及使用它的界面
interface IEmployee{
fullName: string;
}
Run Code Online (Sandbox Code Playgroud)
当通过此接口使用实例时,如果我们尝试分配给fullName,编译器将不会警告我们没有setter,并且JS运行时只是吞下任何赋值并且不会抛出错误.有没有办法将接口成员标记为只有getter或者只有setter?
我看过这篇文章,但它已经很老了,我想知道,如果有什么改进的话.
打字稿中的属性现在可以具有“只读”修饰符,这可以实现所需的结果。
interface IEmployee{
readonly fullName: string;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8533 次 |
| 最近记录: |