Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 在接口中声明受保护的属性_Typescript - Fatal编程技术网

Typescript 在接口中声明受保护的属性

Typescript 在接口中声明受保护的属性,typescript,Typescript,是否可以在TypeScript接口中声明受保护的属性 例如: interface IsDrawable { // protected // <- seems to be unsupported cssClass: string; } class SomeClass implements IsDrawable { protected // <- errors cssClass: string; } 接口是可绘制的{ //受保护//尝试理解任何语言的接口 由于接口是任

是否可以在TypeScript接口中声明受保护的属性

例如:

interface IsDrawable {
  // protected // <- seems to be unsupported
  cssClass: string;
}

class SomeClass implements IsDrawable {
  protected // <- errors
  cssClass: string;
}
接口是可绘制的{
//受保护//尝试理解任何语言的
接口


由于
接口
是任何人都可以用来访问某些类功能的模式,因此它的字段不能是私有的或受保护的

接口是一种契约,意味着你在告诉自己之外的实体你可以做什么和你拥有什么,通过保护它们,你就是在破坏契约

也就是说,如果要强制实现某个范围为受保护的对象,可以使用抽象类来满足这一需要

更新


abstract
无法应用于属性,因此为了实现此功能,cssClass需要是一个返回字符串的方法。

您得到的错误==>error,无法扩大可见性请在此处阅读有关接口的更多信息,但如果您遵循,则问题并非没有根据。设置道具的可见性不愿意在接口中定义的类中的错误仍然会导致TypeScript抛出这个错误,这是误导性的。尽管这样做有效,但值得注意的是抽象类方法带来了固有的限制(您可以实现多个接口,但不能从多个基本抽象类继承-必须使用组合,这反过来会使事情进一步复杂化)