Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong inferred initial value in reduce #59863

Closed
nop33 opened this issue Sep 5, 2024 · 2 comments
Closed

Wrong inferred initial value in reduce #59863

nop33 opened this issue Sep 5, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@nop33
Copy link

nop33 commented Sep 5, 2024

🔎 Search Terms

"reduce initial value", "wrong inferred initial value in reduce"

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/MYewdgzgLgBGCuBbARgUwE4QILvQQwE8YBeGAbQEYAaGeMAE1QDMBLMVemgJhoGYa6jVu04wArAF0AsAChZAG1Sw2LKCzzyAahvioAXHCRp0JGAAZZs0JFgQkiPOiKkEKDNlyEAdOg7xgqAAUgXjAwDQAbjqoAJQkAHwwAN6yMGkwvlDw6GAwocAwANQwgVHyujAA-JXmMbIAvjQqahra5bFAA

💻 Code

const numbersArray = [1, undefined, 2, 3, undefined, 5]

let initialValue: number = 0

const summary = numbersArray.reduce((acc, value) => {
    return acc + (value ?? 0)
}, initialValue)

🙁 Actual behavior

acc is number | undefined instead of number

🙂 Expected behavior

Since an initialValue is provided the type of acc should be inferred from it and not from the first element of the numbersArray.

A value to which accumulator is initialized the first time the callback is called. If initialValue is specified, callbackFn starts executing with the first value in the array as currentValue.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

How does TypeScript infer the type of the accumulator in reduce()?
TypeScript infers the type of the accumulator based on the initial value provided (if any) and the return type of the reducer function. If no initial value is provided, it infers the type from the first element of the array.
https://www.geeksforgeeks.org/typescript-array-reduce-method/

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

MartinJohns commented Sep 5, 2024

I believe this is working as intended. reduce() has several overloads, and the one used does not have an additional type argument for the initialValue because the provided value is compatible with your array type.

A workaround is to explicitly choose the overload with the type argument by providing it: numbersArray.reduce<number>(...)

Duplicate of #39259.
Related: #7014
Related: #36554 (comment)
Related: #36554 (comment)

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 5, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants