所以遇到了另一个模糊的类型脚本错误。
我有以下功能
fetchAllAssets
以发送操作并运行
responses
通过调用的函数
formatAssets
.
错误就在
响应
数组。
export const fetchAllAssets = () => (dispatch: DispatchAllAssets) => {
dispatch(actionGetAllAssets());
return fetchAll([getPrices(), getAvailableSupply()]).then((responses) =>
dispatch(actionSetAllAssets(formatAssets(responses))));
}
这个
格式资产
功能:
export const formatAssets = (responses: IResponseConfig[]) => {
let prices: any;
let availableSupplies: any;
console.log('responses', responses);
responses.forEach((response: IResponseConfig) => {
const { config } = response;
const { url } = config;
if (url.includes('prices')) {
prices = response.data;
} else if (url.includes('dashboard')) {
availableSupplies = cleanAssets(response.data);
}
return {
prices,
availableSupplies
};
});
误差
“[{},{},{},{},{},{},{},{},{},{},{},{}]”类型的参数不能分配给“IResponseconfig[]”类型的参数。
类型{}缺少类型“iresponseconfig”的以下属性:config,data ts(2345)
以下是接口的用途
响应配置
看起来像:
export interface IResponseConfig {
config: {
adapter: any;
baseUrl: string;
headers: {};
maxContentLength: number;
method: string;
params: {
key: string;
}
timeout: number;
transformRequest: {};
transformResponse: {};
url: string;
validateStatus: any;
xsrfCookieName: string;
xsrfHeaderName: string;
};
data: IAssetResponse[];
headers?: {};
request?: XMLHttpRequest;
upload?: XMLHttpRequestUpload;
status?: number;
statusText?: string;
}
以下是答案:
应该是什么形状
响应配置
为了让错误消失?
Fetchall签名
// @TODO Fix any type.
export const fetchAll = (array: any) => Promise.all(array);
我还可以通过使所有的键都是可选的(hack?)来消除错误。
export interface IResponseConfig {
config?: any;
data?: IAssetResponse[];
headers?: any;
request?: XMLHttpRequest;
upload?: XMLHttpRequestUpload;
status?: number;
statusText?: string;
}
什么
IAssetResponse
看起来像(我关心的实际数据)
export interface IAssetResponse {
[key: string]: string | undefined;
currency: string;
price?: string;
availableSupply?: string;
maxSupply?: string;
}