angular - 接口上不存在属性
问题描述
我对接口在 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 出现任何错误。
解决方案
那么这是否意味着当你在 TypeScript 中实现一个接口时,你只能使用接口中声明的属性和方法呢?
不可以。这意味着当你在 TypeScript 中引用具有特定接口的变量时,你只能使用在该变量的接口中声明的属性和方法。
这是 OOP 中的一般概念。
推荐阅读
- python - Python:匹配字符串中的多个子字符串
- encoding - 为什么在 Thrift CompactProtocol 中以可变长度编码交换字节?
- python - 如何验证 pyMetasploit 模块中的 SSL 证书?
- android - 计算使用 tensorflowlite android 检测到的对象数
- java - 如何使用 Spring Boot、JPA 和 Thymeleaf 设置搜索栏
- c++ - 我有这个unicodestring“Param�tres”,è被转换成未知字符。为什么?
- html - 如何在 ngFor* 循环中动态设置名称标签?
- forms - 如何从 Angular 6 中的父组件访问子组件中的表单?
- python-3.x - 如何使用python获取文件大小和文件夹大小
- javascript - 如何用中间的额外空格替换对象的 PascalCase 键?