My array consists of objects structured like this
type AnyType = {
name: 'A' | 'B' | 'C';
isAny:boolean;
};
const myArray :AnyType[] =[
{name:'A',isAny:true},
{name:'B',isAny:false},
]
I am trying to use the reduce
function on myArray
to create an array based on the 'name' property only
const namesArray=myArray.reduce<AnyType['name'][]>((a,b)=>{
return b.name;
},[])
However, I encounter a TypeScript error
(parameter) b: AnyType
BugFinder: No overload matches this call.BugFinder:
Overload 1 of 6, '(callbackfn: (previousValue: ("A" | "B" | "C")[], currentValue: AnyType, currentIndex: number, array: AnyType[]) => ("A" | "B" | "C")[], initialValue: ("A" | "B" | "C")[]): ("A" | ... 1 more ... | "C")[]', gave the following error.BugFinder:
Argument of type '(a: ("A" | "B" | "C")[], b: AnyType) => "A" | "B" | "C"' is not assignable to parameter of type '(previousValue: ("A" | "B" | "C")[], currentValue: AnyType, currentIndex: number, array: AnyType[]) => ("A" | "B" | "C")[]'.BugFinder:
Type '"A" | "B" | "C"' is not assignable to type '("A" | "B" | "C")[]'.BugFinder:
Type '"A"' is not assignable to type '("A" | "B" | "C")[]'.BugFinder:
Overload 2 of 6, '(callbackfn: (previousValue: ("A" | "B" | "C")[], currentValue: AnyType, currentIndex: number, array: AnyType[]) => ("A" | "B" | "C")[], initialValue: ("A" | "B" | "C")[]): ("A" | ... 1 more ... | "C")[]', gave the following error.BugFinder:
Argument of type '(a: ("A" | "B" | "C")[], b: AnyType) => "A" | "B" | "C"' is not assignable to parameter of type '(previousValue: ("A" | "B" | "C")[], currentValue: AnyType, currentIndex: number, array: AnyType[]) => ("A" | "B" | "C")[]'.BugFinder:
Type '"A" | "B" | "C"' is not assignable to type '("A" | "B" | "C")[]'.BugFinder:
Type '"A"' is not assignable to type '("A" | "B" | "C")[]'.
Click here to access the playground