Home > Back-end >  Return an object with properties from function parameter list
Return an object with properties from function parameter list

Time:12-15

I'm trying to create a function that constructs and returns an object with properties that match the array of strings passed in the params.

type Foods = 'apple' | 'banana' | 'pear';

function foodObjects<K extends Foods>(foods: Foods[]): Record<K, number> {
    const ret = {};
    foods.forEach((food, i) => {
        ret[food] = i;
    })
    return ret;
}

foodObjects(['apple', banana']); // should return { apple: 0, banana: 1 }

In this simple example, you pass it an array of fruits and it returns an object with the fruit names as keys. However, I get a warning when defining the ret variable (const ret = {};):

Type '{}' is not assignable to type 'Record<K, number>'

I can resolve that error with the as keyword, but this doesn't feel correct:

const ret = {} as Record<K, number>;

CodePudding user response:

If you want an array to turn into an object, your best bet is to reduce it:

type Foods = "apple" | "banana" | "pear";

function foodObjects<K extends Foods>(foods: Foods[]): Record<K, number> {
  return foods.reduce((acc, food, i) => ({ ...acc, [food]: i }), <Record<K, number>>{});
}

console.log(foodObjects(["apple", "banana"])); // should return { apple: 0, banana: 1 }
  • Related