Solución
solution.tsTypeScript
interface Product {
name: string;
price: number;
stock: number;
category: string;
}
interface Criteria {
category?: string;
maxPrice?: number;
inStock?: boolean;
}
export function findProduct(products: Product[], criteria: Criteria): Product | undefined {
// Retorna el primer producto que cumpla todos los criterios indicados
// console.log(products, criteria)
return products.find( (productObj) => {
if( criteria.hasOwnProperty('category')
&& productObj['category'] !== criteria['category']) {
return false;
}
if( criteria.hasOwnProperty('maxPrice')
&& productObj['price'] >= criteria['maxPrice']) {
return false;
}
if( criteria.hasOwnProperty('inStock')
&& productObj['stock'] < 0 ) {
return false;
}
return true;
}) || null;
}0respuestas