我正在从客户端的角度GET请求到服务器。在我提出的呼叫服务需要返回一个可观察与对象的数组。问题是,打字稿似乎并没有让我的JSON投给我的接口。
这是JSON对象服务器发送作为响应:
export interface Message {
title: string;
body: string;
}
这是我想给体投地的接口。有主体应该包含这些对象的数组:
export interface ICommonGameCard {
id: string;
pov: POVType;
title: string;
image_pair?: ICommonImagePair;
best_time_solo: number[];
best_time_online: number[];
}
export enum POVType{Simple, Free};
这个服务发出请求:
public getGameCards(povType: POVType): Observable<ICommonGameCard[]> {
return this.http.get<ICommonGameCard[]>(this.BASE_URL + this.GET_ALL_CARDS_URL)
.pipe(
map((res: Response) => return <ICommonGameCard>res.json().body),
catchError(this.handleError<Message>("getUsernameValidation")),
);
}
显然,这是行不通的。我想投响应的JSON的消息接口,然后访问该消息的接口主体那里是ICommonGameCard的数组。
我得到这个错误:
[ts]
Argument of type 'OperatorFunction<Response, ICommonGameCard>' is not assignable to parameter of type 'OperatorFunction<ICommonGameCard[], ICommonGameCard>'.
Type 'Response' is missing the following properties from type 'ICommonGameCard[]': length, pop, push, concat, and 26 more. [2345]
究竟什么是错我的语法?
export interface Message {
title: string;
body: ICommonGameCard[];
}
public getGameCards(povType: POVType): Observable<ICommonGameCard[]> {
return this.http.get<Message>(this.BASE_URL + this.GET_ALL_CARDS_URL)
.pipe(
map((message: Message) => message.body),
catchError(this.handleError<Message>("getUsernameValidation"))
);
}