I'm trying to use async
await
with reduce
function in TypeScript:
const getMyItems = async (items: MyType[]) => {
return await items.reduce(async (allItems, currItem) => {
const shouldAddItem = await someCheck(currItem);
return [...allItems, ...(shouldAddItem ? [currItem] : [])];
}, []);
};
I get error on this line:
return await items.reduce(async (allItems, currItem) => {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error message:
TS2769: No overload matches this call. Overload 1 of 3, '(callbackfn: (previousValue: InviteASItemProps, currentValue: InviteASItemProps, currentIndex: number, array: InviteASItemProps[]) => InviteASItemProps, initialValue: InviteASItemProps): InviteASItemProps', gave the following error. Argument of type '(filteredItems: never[], currentItem: InviteASItemProps) => Promise' is not assignable to parameter of type '(previousValue: InviteASItemProps, currentValue: InviteASItemProps, currentIndex: number, array: InviteASItemProps[]) => InviteASItemProps'. Types of parameters 'filteredItems' and 'previousValue' are incompatible. Type 'InviteASItemProps' is missing the following properties from type 'never[]': length, pop, push, concat, and 29 more. Overload 2 of 3, '(callbackfn: (previousValue: never[], currentValue: InviteASItemProps, currentIndex: number, array: InviteASItemProps[]) => never[], initialValue: never[]): never[]', gave the following error. Argument of type '(filteredItems: never[], currentItem: InviteASItemProps) => Promise' is not assignable to parameter of type '(previousValue: never[], currentValue: InviteASItemProps, currentIndex: number, array: InviteASItemProps[]) => never[]'. Type 'Promise' is missing the following properties from type 'never[]': length, pop, push, concat, and 29 more.
Any ideas why?
When I use allItems: any
it's fixed.