Home > database >  Declare variable number typescript
Declare variable number typescript

Time:06-03

Hello guys im new in typescript, my Array Item like this [string,number] but when i Declare item:[string,number] typescript take error bellow like this, but i declare item:[string,any] then no error. Any ideal? Thank you guys

 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)

CodePudding user response:

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;
  },
  {}
);
  • Related