类型“ {}”缺少类型“ IResponseConfig”

时间:2019-03-06 20:30:13

标签: javascript typescript

所以遇到另一个模糊的打字稿错误。

我有以下函数fetchAllAssets,该函数以调度动作并通过名为responses的函数运行formatAssets结束。

错误在responses数组上。

export const fetchAllAssets = () => (dispatch: DispatchAllAssets) => {
  dispatch(actionGetAllAssets());
  return fetchAll([getPrices(), getAvailableSupply()]).then((responses) =>
    dispatch(actionSetAllAssets(formatAssets(responses))));
}

formatAssets 函数:

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
    };
  });

错误

enter image description here

  

类型'[{},{},{},{},{},{},{},{},{},{}]'的参数不能分配给类型'IResponseConfig []的参数'。     类型“ {}”缺少类型“ IResponseConfig”中的以下属性:config,data ts(2345)

IResponseConfig 的界面如下所示:

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;
}

以下是答复:

enter image description here

IResponseConfig 应该采用什么形状才能使错误消失?

fetchAll的签名

// @TODO Fix any type.
export const fetchAll = (array: any) => Promise.all(array);

我还可以通过将所有键设置为可选(删除?)来消除该错误

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;
}

1 个答案:

答案 0 :(得分:1)

您似乎需要将<ion-item (click)="saveContact()"> 定义为通用函数,如下所示:

fetchAll

注意:请参阅this question,以获取有关export const fetchAll = <T extends {}>(array: (T | Promise<T>)[]) => Promise.all(array); 解析失败的原因的讨论。

假设<T>(array T[]) => ...getPrices都返回getAvailableSupplyIResponseConfig,则参数Promise<IResponseConfig>的类型为{{1 }}。

如果不是这种情况,则可能必须添加一些其他转换,键入提示或断言以将其强制为一致的接口。