【发布时间】:2019-07-22 00:51:33
【问题描述】:
我对接口在 TypeScript 中的工作方式感到困惑。
这是一段代码:
interface test1 {
}
class test2 implements test1
{
public foo;
}
let test: test1 = new test2();
test.foo = 'test';
不会编译报错“property foo does not exist on type test1。那是不是意味着你在TypeScript中实现接口时,只能使用接口中声明的属性和方法?
我很困惑,因为我习惯了 PHP,这不会导致 PHP 出现任何错误。
【问题讨论】:
-
正如 zmag 所说,这就是 OOP 中的一般思想。 (您可以阅读有关协变和逆变的内容,这很有趣;))
标签: angular typescript