I encountered a problem with my code that I need help fixing. Here is a simple example:
interface employer {
name: string;
age: number;
}
const arr: employer[] = [{name:'Amy',age:18},{name:'Bob',age:20}];
let data = arr.reduce((c, b) => b.age > 18 ? [...c, b] : c,[])
console.log(data)
The goal here is to filter the arr
array and return the ages of people who are older than 18. However, I am running into errors:
No overload matches this call.
Overload 1 of 3, '(callbackfn: (previousValue: employer, currentValue: employer, currentIndex: number, array: employer[]) => employer, initialValue: employer): employer', gave the following error.
Type 'employer[]' is missing the following properties from type 'employer': name, age
Overload 2 of 3, '(callbackfn: (previousValue: never[], currentValue: employer, currentIndex: number, array: employer[]) => never[], initialValue: never[]): never[]', gave the following error.
Type 'employer[]' is not assignable to type 'never[]'.
Type 'employer' is not assignable to type 'never'.
In the line b.age > 18 ? [...c, b] : c
, it seems like it should work in JavaScript. How can we fix this issue? Thank you!