Typescript返回类型的接口,而不是类型

时间:2020-09-03 15:33:58

标签: javascript typescript

所以我有以下课程:

export abstract class SuperIO {
    async http<T>(
        request: RequestInfo
    ): Promise<IHttpResponse<T>> {
        const response: IHttpResponse<T> = await fetch(
            request
        );
        try {
            response.parsedBody = await response.json();
        } catch (ex) {
        }

        if (!response.ok) {
            throw new Error(response.statusText);
        }
        return response;
    }

    async get<T>(
        path: string,
        body: any,
        args: RequestInit = {method: "get", body: JSON.stringify(body)}
    ): Promise<IHttpResponse<T>> {
        return await this.http<T>(new Request(path, args));
    };
}

IHttpResponse如下所示:

interface IHttpResponse<T> extends Response{
    parsedBody?: T;
}

现在我希望使用此类,因此我创建了以下内容:

import {SuperIO} from "../Framework/SuperIO";


export interface IContentData {
    id: number;
    url: string;
    htmlTag: string;
    importJSComponent: string;
    componentData: string
}

export class ContentIOService extends SuperIO {

    public async GetContent(url: string) {
        const response = await super.get<IContentData>(url, {});

        this.ProcessResponse(response);

    }

    private ProcessResponse(ContentData: IContentData) {

    }
}

但是在this.ProcessResponse处出现以下错误:

TS2345:“ IHttpResponse”类型的参数不能分配给“ IContentData”类型的参数。类型“ IHttpResponse”缺少类型“ IContentData”中的以下属性:id,htmlTag,importJSComponent,componentData

有人可以告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

getLists的类型为const response

您需要将IHttpResponse<IContentData>传递给您的方法。

response.parsedBody