简体   繁体   English

Angular/Typescript 接口中的类型

[英]Types in Angular/Typescript interfaces

In my app I have BaseObjectInterface and I extend it in many interfaces like AObjInterface , BObjInterface , ect.在我的应用程序中,我有BaseObjectInterface并在AObjInterfaceBObjInterface等许多接口中扩展它。 Then I have parent interface that has array of BaseObjectInterface in it.然后我有父接口,其中包含BaseObjectInterface数组。 I want it to accept all classes that extend base one.我希望它接受所有扩展基类的类。 For example AObjInterface , BObjInterface , etc. Sadly it doesn't work that way.例如AObjInterfaceBObjInterface等。遗憾的是它不能那样工作。 Can I do it any other way than explicitly listing all types?除了明确列出所有类型之外,我还能以其他方式做到这一点吗?

What I have: list: Array<BaseObjectInterface>我有什么: list: Array<BaseObjectInterface>

What I don't want: list: Array<AObjInterface|BObjInterface>我不想要的: list: Array<AObjInterface|BObjInterface>

What I want put in that list: [ New BaseObjectInterface(), new AObjInterface(), new BObjInterface()]我想要放入该列表的内容: [ New BaseObjectInterface(), new AObjInterface(), new BObjInterface()]

If, as you say, BaseObjectInterface is a parent of AObjInterface and BObjInterface, then the type can be just Array<BaseObjectInterface> .如您所说,如果 BaseObjectInterface 是 AObjInterface 和 BObjInterface 的父级,则类型可以只是Array<BaseObjectInterface> TypeScript Playground example TypeScript 游乐场示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM