【发布时间】:2018-10-17 08:31:45
【问题描述】:
我为对象数组创建了一个接口
export interface UpdateMediaList {
[index: number]: ContactResponse;
}
interface ContactResponse {
contactId: number;
email: string;
firstName: string;
imgDisplayPath: string;
inMediaList: true;
jobTitle: string;
lastName: string;
mediaOutletId: number;
mediaOutletName: string;
openRate: string;
phone: string;
responseRate: string;
secureRate: string;
}
然后我在我的服务中使用它...
postUpdateMediaList(param, body, url = this.urls.mediaList.updateMediaList) {
let params = new HttpParams();
params = params.set('sortType', param.sortType);
params = params.set('numRows', param.numRows);
params = params.set('startRow', param.startRow);
return this.http.post<UpdateMediaList>(url, body, {params: params});
}
现在我收到了这个错误
ERROR in src/app/modules/media-list/containers/media-list-builder/media-list-builder.component.ts(142,22): error TS2339: Property 'filter' does not exist on type 'UpdateMediaList'
在这段代码中:
this.mediaList = response
.filter(items => items.inMediaList === true)
.map(items => items.contactId);
while (this._contacts.value.length !== 0) {
this._contacts.removeAt(0);
}
【问题讨论】:
-
就用
type UpdateMediaList = ContactResponse[]? -
@jcalz “只需使用类型 UpdateMediaList = ContactResponse[]?”在哪里?
-
而不是您当前对
UpdateMediaList的定义,这只是说“这个东西具有带有ContactResponse值的数字键属性”而不是“这是一个ContactResponse值的数组”。一个实际的数组具有所有这些数组方法,例如filter()。数字索引的对象可能不会。 -
即所有数组都具有数字索引属性,但并非所有具有数字索引属性的对象都是数组。
-
您检查过响应的类型吗?我怀疑它是数组类型
标签: angular typescript