I have inherited a code with a lot of duplication, basically I have 4 different types and for all of them I have to do the same actions. I have managed to refactor most of my code and got stuck to mapping part. Basically, I need to take each object, map it to its corresponding type and save it to a corresponding array. I wanted to put all my constructor calls for each type in an array, iterate over that array and return a function in which I can pass some parameters.
something like this:
arrayOfArraysToStoreValues = [frstArray, secondArray, thirdArray]
getGoodConstructor(index, firstParam, secondParam) {
const arrayOfConstructors = [ new typeOne(firstParam, secondParam), new
typeTwo(firstParam, secondParam), new TypeThree(firstParam, secondParam)]
return arrayOfConstructors[index]
}
and I wanted to iterate over it inside my forEach loop
arrayOfArraysToStoreValues[index].push(getGoodConstrucor(index, firstParam, secondParam))
But when I try to do it like this I have an error:
Argument of type ‘type1 | type3 | Type3’ is not assignable to parameter of type ‘Type1 & Type2 & Type3’.
(notice difference between | and & )
Can I do something like this? Is there any other solution that might help me refactor and avoid duplication?
Thank you