0

I have two lists like these:

arr1 = [object, object, object...]
arr2 = [10, 2, 5,...]

and combined them using zip:

let zip = arr1.map((x:object, i:number) => [x, arr2[i]]);
// [[object, 10], [object, 2], [object, 5],...]

Then, I want to apply a map on the zip like this, for example:

zip.map((item) => {
  a = item[0] // object
  b = item[1] // number
})

The 'item' in the code above implicitly has an 'any' type, so I want to define the type like:

item: {object, number}[] // <- imaginary syntax

but this doesn't work. Does anyone know how to define the type, for a case like this? I can solve the error, by simply write it as item: any[], but I don't want to use 'any' in my code.

1
  • 1
    item: [object, number]
    – VLAZ
    Sep 15, 2022 at 5:46

1 Answer 1

0

Your "imaginary syntax" is very close to the real syntax: [object, number], and for an array of these arrays, [object, number][].

const arr1 = [{}, {}, {}];
const arr2 = [10, 2, 5];

let zip: [object, number][] = arr1.map((x, i) => [x, arr2[i]]);

zip.map((item) => {
  const a = item[0] // object
  const b = item[1] // number
});

TypeScipt Playground

1
  • Thank you. The error is fixed.
    – shinchan97
    Sep 15, 2022 at 6:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.