I'm struggling with defining type for accumulator in array reduce method.
private buildRequestBody(payoutFields: IWithdrawalRequest): ISignedWithdrawalRequest {
const sortedPayoutFieldKeys = Object.keys(payoutFields)
.sort() as Array<keyof IWithdrawalRequest>;
const requestBody = sortedPayoutFieldKeys.reduce((acc: Partial<IWithdrawalRequest>, key) => {
acc[key] = payoutFields[key] ;
return acc;
}, {});
...
}
It keeps throwing a typescript error for acc[key]
TS2322: Type 'string | number' is not assignable to type 'undefined'. Type 'string' is not assignable to type 'undefined'.
payoutFields, right ? why do you bother to sort its properties ? anyway, if you must do this sorting, you could at least useObject.fromEntriesto replace that old accumulator pattern, which would remove your typing issue altogether and improve efficiency.