我有一个地址簿和两种类型的联系人:
-
人
-
组织
它们都延伸到了 Contact 类 .
我想要一个类的方法 return an array of contacts .
public method(args...): Array<Contact> {...}
很好,但如果我有这个方法到数组内的 do something with some properties of the Contacts 怎么办?
public method(args...): Array<Contact> {
contacts :Array<Contact> = getContactsFromSomewhere();
contacts.forEach( contact => {
if(typeof contact === "Person"){ //persons and organizations are different, they have different atrributes
//Here i modify some Person attributes
}
else{
//Here i modify some Organization attributes
});
return contacts;
这感觉很麻烦,难以阅读 . Why not something like this?
public method(args...): Array<T extends Contact> {
contacts :Array<T extends Contact> = getContactsFromSomewhere();
contacts.forEach( contact => {
contact.doSomething() //abstract method implemented in both classes its own way.
});
return contacts;
不可能,我 can't do this
Array<T extends Contact>
这个 error 由VSCode抛出:
[ts]通用类型'Array'需要1个类型的参数
2 回答
您似乎根据您的问题知道问题的答案,如您所写:
你可以使用你已经拥有的相同方式,没有“麻烦”的部分 .
例如,如果要过滤掉以字符串开头的联系人,那么只需要
Contact
的startsWith
方法并在所有子类中实现它,然后当您迭代联系人时,此方法始终可用,并且无需检查特定的类型:由于您可能在基础
Contact
类中具有联系人名称,因此不需要它是抽象的,而只是为了示例 .如果您需要以不同方式处理联系人,则有3个选项:
(1)像你一样检查类型:
(2)有不同的联系人“getters”过滤联系人数组:
(3)针对不同类型具有不同的数据结构,然后执行与(2)中相同的操作 .
关于您的代码的另外两条评论:
在您的方法中使用
var
或let
作为contacts
变量,否则您将污染全局名称空间使用
instanceof
而不是typeof
因为:(code in playground)
您不能声明这样的通用签名,在您的示例中,typescript认为您要使用Array的通用签名,并且 T extends Contact 不是类型,它是通用签名 .
你可以这样做: