I am facing a challenge with my reduce
function and I have tried multiple solutions without success:
interface ITask {
id: string;
was: string;
}
//sampleData:
const tasks = [
{id: "a", was: "Foo"},
{id: "b", was: "Foo"},
{id: "c", was: "Bad"}
];
const uniqueList = tasks.reduce<>((acc, current) => {
const x = acc.find((item: ITask) => item.was === current.was);
return !x ? acc.concat(current) : acc;
}, []);
This results in the following errors:
Property 'find' does not exist on type 'never'.
Property 'was' does not exist on type 'never'.
Property 'concat' does not exist on type 'never'.
The issue appears to be related to the types involved, where current
is of type ITask
and the accumulator
is of type ITask[]|[]
. To address this, I attempted:
const uniqueList = tasks.reduce<>((acc: ITask[] | [], current: ITask) => {
const x = acc.find((item: ITask) => item.was === current.was);
return !x ? acc.concat(current) : acc;
}, []);
However, I encountered the following error:
Argument of type '(acc: ITask[] | [], current: ITask) => ITask[]' is not assignable to parameter of type '(previousValue: never, currentValue: never, currentIndex: number, array: never[]) => never'.
Type 'ITask[]' is not assignable to type 'never'.
Argument of type 'ITask' is not assignable to parameter of type 'ConcatArray<never>'.
Type 'ITask' is missing the following properties from type 'ConcatArray<never>': length, join, slice
As suggested in the comments, I tried:
const uniqueList = tasks.reduce((acc, current: ITask) => {
const x = acc.find((item: ITask) => item.was === current.was);
return !x ? acc.concat(current) : acc;
}, [] as ITask[] | []);
Unfortunately, I still received the following errors:
Property 'find' does not exist on type 'never'.
Property 'concat' does not exist on type 'never'.