声明可变数字打字稿

Declare variable number typescript

大家好,我是 typescript 的新手,我的 Array Item 是这样的 [string,number] 但是当我声明 item:[string,number] typescript 时会出现这样的错误,但是我声明 item:[string,any] 然后没有错误。有理想吗?谢谢大家

 const allResults = Object.entries(data.rates).reduce(
  (
    obj: {
      [key: string]:
        | string
        | { rate: string; value: string; currency: string };
    },
    item: [string, any]
  ) => {
    console.log(typeof item[1]); //number
    obj = {
      ...obj,
      [item[0]]: {
        rate: item[0],
        value: (item[1] * amount).toFixed(2),
        currency: item[1].toFixed(2),
      },
    };
    return obj;
  },
  {}
);

No overload matches this call. Overload 1 of 3, '(callbackfn: (previousValue: [string, unknown], currentValue: [string, unknown], currentIndex: number, array: [string, unknown][]) => [string, unknown], initialValue: [string, unknown]): [string, unknown]', gave the following error. Argument of type '(obj: { [key: string]: string | { rate: string; value: string; currency: string; }; }, item: [string, number]) => { [key: string]: string | { rate: string; value: string; currency: string; }; }' is not assignable to parameter of type '(previousValue: [string, unknown], currentValue: [string, unknown], currentIndex: number, array: [string, unknown][]) => [string, unknown]'. Types of parameters 'obj' and 'previousValue' are incompatible. Type '[string, unknown]' is not assignable to type '{ [key: string]: string | { rate: string; value: string; currency: string; }; }'. Index signature for type 'string' is missing in type '[string, unknown]'. Overload 2 of 3, '(callbackfn: (previousValue: { [key: string]: string | { rate: string; value: string; currency: string; }; }, currentValue: [string, unknown], currentIndex: number, array: [string, unknown][]) => { [key: string]: string | { ...; }; }, initialValue: { ...; }): { ...; }', gave the following error. Argument of type '(obj: { [key: string]: string | { rate: string; value: string; currency: string; }; }, item: [string, number]) => { [key: string]: string | { rate: string; value: string; currency: string; }; }' is not assignable to parameter of type '(previousValue: { [key: string]: string | { rate: string; value: string; currency: string; }; }, currentValue: [string, unknown], currentIndex: number, array: [string, unknown][]) => { [key: string]: string | { ...; }; }'. Types of parameters 'item' and 'currentValue' are incompatible. Type '[string, unknown]' is not assignable to type '[string, number]'. Type at position 1 in source is not compatible with type at position 1 in target. Type 'unknown' is not assignable to type 'number'.ts(2769)

type Rate = {
  [key in string]: number
}

const data: any = {}
const amount = 1 // no idea where is coming from in your example

const allResults = Object.entries(data.rates as Rate).reduce(
  (
    obj: {
      [key: string]:
        | string
        | { rate: string; value: string; currency: string };
    },
    item: [string, number]
  ) => {
    obj = {
      ...obj,
      [item[0]]: {
        rate: item[0],
        value: (item[1] * amount).toFixed(2),
        currency: item[1].toFixed(2),
      },
    };
    return obj;
  },
  {}
);