The following TypeScript works:
const carsAndTrucks = { cars: [], trucks: [] };
const { cars, trucks } = mapDealerInventory();
carsAndTrucks.cars = cars
carsAndTrucks.trucks = trucks
console.log(carsAndTrucks)
function mapDealerInventory() {
return {cars: [1,2], trucks:['a', 'b']}
}
But is there a way to avoid having to set new variables just to destructure the return value of mapDealerInventory()
? I was hoping this was possible:
{ carsAndTrucks.cars, carsAndTrucks.trucks } = mapDealerInventory()
but it gives the error "Unexpected token ="
So if you declare an object with its property types first, what's the cleanest way to set those properties from a function that returns their values in an object (or an array)?