Skip to content

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

Closed
pkasarda opened this issue Sep 12, 2022 · 7 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@pkasarda
Copy link

pkasarda commented Sep 12, 2022

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 code

undefined

nullish

💻 Code

// We can quickly address your report if:
//  - The code sample is short. Nearly all TypeScript bugs can be demonstrated in 20-30 lines of code!
//  - It doesn't use external libraries. These are often issues with the type definitions rather than TypeScript bugs.
//  - The incorrectness of the behavior is readily apparent from reading the sample.
// Reports are slower to investigate if:
//  - We have to pare too much extraneous code.
//  - We have to clone a large repo and validate that the problem isn't elsewhere.
//  - The sample is confusing or doesn't clearly demonstrate what's wrong.

🙁 Actual behavior

it shows prevValue as number

🙂 Expected behavior

it should show prevValue as initialValue type

@fatcerberus
Copy link

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.

@fatcerberus
Copy link

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

0
1
2
3
4
5

number is the correct type for currentValue here.

@pkasarda
Copy link
Author

pkasarda commented Sep 12, 2022

sorry everyone... I was in rush :( issue is/was with prevValue not with initialValue... and steps are HERE not sure but when object can be undefined then it's talking about number from somewhere...

but fixed even when I'm using it like this

@pkasarda pkasarda changed the title Array.reduce currentValue is always type of array and not of initialValue type Array.reduce prevValue is type of array and not of initialValue type when we return undefined Sep 12, 2022
@andrewbranch
Copy link
Member

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 reduce inference, it’s probably a duplicate of #46438

@andrewbranch andrewbranch added the Needs More Info The issue still hasn't been fully clarified label Sep 12, 2022
@fatcerberus
Copy link

fatcerberus commented Sep 12, 2022

@andrewbranch Yeah, I have no idea either - OP now says prevValue is the wrong type (number instead of ArrObj), but the playground links show prevValue correctly being inferred as ArrObj, so...

It seems like the actual problem is OP's mistake - initialValue is an ArrObj and so that's what the callback is expected to return, but it might in fact return something nullish, causing a type error. It seems suspiciously like a gross misunderstanding of what reduce actually does.

@pkasarda
Copy link
Author

@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 pkasarda closed this as not planned Won't fix, can't repro, duplicate, stale Sep 13, 2022
@fatcerberus
Copy link

@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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

3 participants