-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Array.reduce prevValue is type of array and not of initialValue type when we return undefined #50729
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
initialValue is the initial value of previousValue, not currentValue. currentValue is always an element of the source array, so the type looks correct to me. |
const arr: number[] = [0,1,2,3,4,5];
type ArrObj = {
subRows: ArrObj[];
}
const arrObj: ArrObj[] = [
{ subRows: [] as ArrObj[] },
{ subRows: [] as ArrObj[] },
]
const result = arr.reduce((prevValue, currentValue) => {
console.log(currentValue);
return null as any as ArrObj;
}, arrObj[0]); prints
|
Through the edits and updates, I have no idea what the issue is here or if you still have one, but if you’re unsatisfied with |
@andrewbranch Yeah, I have no idea either - OP now says It seems like the actual problem is OP's mistake - |
@andrewbranch yes, it looks like a duplicate of #46438 @fatcerberus forget to remove comment from playground, I updated the link in my original comment... |
@pkasarda If this is a duplicate of #46438 then your issue was the type of the result, not the prevValue/currentValue (which seem to be inferred correctly in all of your examples). Even more to the point, the inferred types don’t matter, since there was an error (“no overload matches this call”) on the callback that you didn’t mention. So it wasn’t at all clear what you were even trying to do. |
Bug Report
Array.reduce is not using the proper type when working with prevValue, it should have type of initialValue but it doesn't
🔎 Search Terms
reduce, array, initialValue, currentValue
🕗 Version & Regression Information
all versions also nightly
⏯ Playground Link
Playground link with relevant codeundefined
nullish
💻 Code
🙁 Actual behavior
it shows prevValue as number
🙂 Expected behavior
it should show prevValue as initialValue type
The text was updated successfully, but these errors were encountered: