Javascript Typescript返回类型为而不是类型的接口

Javascript Typescript返回类型为而不是类型的接口,javascript,typescript,Javascript,Typescript,因此,我有以下课程: export abstract class SuperIO { async http<T>( request: RequestInfo ): Promise<IHttpResponse<T>> { const response: IHttpResponse<T> = await fetch( request ); try {

因此,我有以下课程:

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));
    };
}
现在我希望使用这个类,因此我创建了以下内容:

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) {

    }
}
从“./Framework/SuperIO”导入{SuperIO};
导出接口IContentData{
id:编号;
url:string;
htmlTag:字符串;
importJSComponent:字符串;
组件数据:字符串
}
导出类ContentIOService扩展了SuperIO{
公共异步GetContent(url:string){
const response=wait super.get(url,{});
这个.ProcessResponse(响应);
}
私有ProcessResponse(ContentData:IContentData){
}
}
但是在
this.ProcessResponse
中,我得到以下错误:

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


谁能告诉我我做错了什么吗?

const response
属于
IHttpResponse

您需要将
response.parsedBody
传递给您的方法

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

        response?.parsedBody && this.ProcessResponse(response.parsedBody);

    }

    private ProcessResponse(ContentData: IContentData) {

    }
公共异步GetContent(url:string){ const response=wait super.get(url,{}); response?.parsedBody&&this.ProcessResponse(response.parsedBody); } 私有ProcessResponse(ContentData:IContentData){ }
我认为您上面的错误已被删除。TS2345:类型为“IHttpResponse”的参数不能分配给类型为“IContentData”的参数。类型“IHttpResponse”…?完整错误为:TS2345:类型“IHttpResponse”的参数不能分配给类型“IContentData”的参数。类型“IHttpResponse”缺少类型“IContentData”中的以下属性:id、htmlTag、importJSComponent、ComponentData如果我这样做,我会得到:TS2345:类型“IContentData | undefined”的参数不能分配给类型“IContentData”的参数。类型“undefined”不可分配给类型“IContentData”。这是因为parsedBody可能未定义(参数后面有一个“?”)。您可以先检查它是否存在。我更新了我的答案。谢谢你@Todd Skelton。有什么方法可以改进和避免检查吗?
    public async GetContent(url: string) {
        const response = await super.get<IContentData>(url, {});

        response?.parsedBody && this.ProcessResponse(response.parsedBody);

    }

    private ProcessResponse(ContentData: IContentData) {

    }