したがって、次のクラスがあります:
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
誰か私が間違ったことをしたのか教えてくれませんか?
私はあなただと思います上のエラーは切り取られます。 TS2345: タイプ「IHttpResponse」の引数タイプ「IContentData」のパラメータには割り当てられません。 「IHttpResponse」と入力します...?
– bnieland
2020 年 9 月 3 日 15:43
エラー全体は次のようになります: TS2345: タイプ 'IHttpResponse<IContentData' の引数タイプ「IContentData」のパラメータには割り当てられません。 「IHttpResponse」と入力します。タイプ 'IContentData' に次のプロパティがありません: id、htmlTag、importJSComponent、componentData
– マーク・ラスムッセン
2020 年 9 月 3 日 16:13
------------------------
const レスポンスのタイプは IHttpResponse<IContentData> です。
response.parsedBody をメソッドに渡す必要があります。
public async GetContent(url: string) {
const response = await super.get<IContentData>(url, {});
response?.parsedBody && this.ProcessResponse(response.parsedBody);
}
private ProcessResponse(ContentData: IContentData) {
}
3
これを実行すると、次の結果が得られます: TS2345: Argument of type 'IContentData |未定義'タイプ「IContentData」のパラメータには割り当てられません。タイプ「未定義」タイプ には割り当てられません9;Iコンテンツデータ'。
– マーク・ラスムッセン
2020 年 9 月 3 日 16:51
これは、parsedBody が未定義である可能性があるためです (パラメーターの後に ? が付いています)。最初に存在するかどうかを確認できます。回答を更新しました。
– トッド・スケルトン
2020 年 9 月 3 日 16:53
ありがとう@Todd Skelton。改善してチェックを回避する方法はありますか?
– マーク・ラスムッセン
2020 年 9 月 3 日 17:10