angular - 如何在 TypeScript 的函数定义中用子类型替换类型?
问题描述
interface Parent {
name: string;
}
interface Child extends Parent {
name: string;
text: string;
}
function myFunction(text: string, target: Child): Child {
target.text = text;
console.log(target);
return target;
}
const testChild: Child = {
name: 'test',
text: 'sample',
};
declare type FunctionType = (text: string, target: Parent) => Parent;
const func: FunctionType = myFunction;
func('newText', testChild);
此代码会导致错误,因为 FunctionType 等待具有 Parent(而不是 Child)作为参数和结果之一的函数。有没有办法让它与 Parent 的直系后代一起工作(就像 Child 一样)?
什么时候可能有用?假设我们有一个 Angular 组件,它接受简单的 slim 项目(比如我们的 Parent)和处理函数,但是我们想给这个组件一个 Child 项目和相应的处理函数(在我们的例子中是 myFunction)。所有组件的内部功能仅涉及与父相关的字段。我知道可以在 Angular 中为父组件的类创建一个子类并覆盖属性。但是为每个新的“子”类型创建一个新的组件或类会很痛苦。这只是一个例子。
我的一般问题是如何定义不仅接受父接口,还接受所有子接口(不列出子接口)的函数类型?
实际上我正在尝试实施基本的替代原则:对父母有用的东西应该对孩子有用。
解决方案
推荐阅读
- php - htaccess 在子目录中的 Wordpress 中返回 404(桌面可以,移动 404)
- xml - doxygen 无法将项目信息输出到 xml 文件
- javascript - 如何检查 React 树,查看 Internet Explorer 11 中组件的属性和状态?
- kendo-ui - Kendo UI 网格支持 OData v4 $filter IN 运算符?
- r - 为什么我在使用 R 函数 gsynth 时会出现此错误?
- mysql - 如何计算超过 1 个有子句 mysql
- mysql - mysql,避免子查询中的限制
- c++ - 如何在c ++中将用户输入存储到数组
- java - 在JAVA中使用字符串的回文
- vb.net - 哪些参数会影响 autoreset.set() 和 autoreset 从等待句柄退出之间