I'm trying to create a function with generic type in typescript. I have these function whose do the same
const convertBeverages = (beveragesCollection: BeveragesQuery): Beverage[] => {
let beverages: Beverage[] = [];
if (beveragesCollection) {
beverages =
beveragesCollection.data.map(({ id, attributes }) => {
const restaurant =
attributes?.restaurant?.reduce((acc, currentValue) => {
const restaurantId = currentValue?.restaurant?.data?.id ?? "-1";
return currentValue && currentValue.available
? [...acc, { price: currentValue.price, restaurantId }]
: acc;
}, [] as PriceRestaurant[]) ?? [];
const beverage = {
id: id ?? "",
name: attributes?.name ?? "",
position: attributes?.position ?? 0,
restaurant,
};
return beverage;
}) ?? [];
}
return beverages;
};
const convertDesserts = (dessertCollection: DessertsQuery): Dessert[] => {
let desserts: Dessert[] = [];
if (dessertCollection) {
desserts =
dessertCollection.data.map(({ id, attributes }) => {
const restaurant =
attributes?.restaurant?.reduce((acc, currentValue) => {
const restaurantId = currentValue?.restaurant?.data?.id ?? "-1";
return currentValue && currentValue.available
? [...acc, { price: currentValue.price, restaurantId }]
: acc;
}, [] as PriceRestaurant[]) ?? [];
const dessert: Dessert = {
id: id ?? "",
name: attributes?.name ?? "",
position: attributes?.position ?? 0,
restaurant,
};
return dessert;
}) ?? [];
}
return desserts;
};
So i want to create a generic function but i have an error and i don't know why.
Basically , i copied the previous function and I added the TData with the input for the previous functions and the TModel for the value of return
