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

Array.reduce doesn't work #44063

Closed
ilchenkoArtem opened this issue May 12, 2021 · 5 comments
Closed

Array.reduce doesn't work #44063

ilchenkoArtem opened this issue May 12, 2021 · 5 comments
Labels
Unactionable There isn't something we can do with this issue

Comments

@ilchenkoArtem
Copy link

ilchenkoArtem commented May 12, 2021

Bug Report

πŸ”Ž Search Terms

Array.reduce
Array.reduce doesn't work

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about reduce

⏯ Playground Link

Playground link with relevant code

πŸ’» 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

TS throws error "This expression is not callable."

πŸ™‚ Expected behavior

TS should let me iterate through array (I tell it the type is either array of IBaseOption OR array of ISelectOption (not an array of mixed types). In any case the variable is an array and those methods should be callable.

@ilchenkoArtem ilchenkoArtem changed the title Array.reducer don't work Array.reduce don't work May 12, 2021
@ilchenkoArtem ilchenkoArtem changed the title Array.reduce don't work Array.reduce doesn't work May 12, 2021
@jcalz
Copy link
Contributor

jcalz commented May 12, 2021

The example code should be here as plain text, and phrases like "doesn't work" and "works" aren't really descriptive enough to do anybody much good. Could you make an edit?

Note that this seems to be a duplicate of or strongly related to #36390; that issue is closed as fixed, although the problem with reduce() remains; not sure if this one should stay open in its place or if there's a different canonical issue somewhere.

@RyanCavanaugh RyanCavanaugh added the Unactionable There isn't something we can do with this issue label May 12, 2021
@RyanCavanaugh
Copy link
Member

I think what you want is this (which has two edits)

TEST_LIST.map((testListItem) => {
  const options = testListItem.options;
  (options as IBaseOption[]).reduce<ValueType[]>((acc, option) => {
    if(option.parentValue) {
      acc.push(option.parentValue)
    }
   
    return acc;
  }, []);
})

There's no typesafe way for TS to collapse the different reduce definitions without knowing what reduce "actually does"; requiring a type assertion here is appropriate.

@Lafore17
Copy link

But one question reading comments above: considering options as IBaseOption[] | ISelectOption[] I expect to get type for option inside reduce like IBaseOption | ISelectOption and at the same time never[] for accum before setting type via generic. am I right?

@ialexryan
Copy link
Contributor

I poked around in the files of #31023, which fixed .map() on unions of array types (closing #36390), and found this comment in the test suite:

https://github.com/microsoft/TypeScript/pull/31023/files#diff-7a118e355c5fce1fa4374a0e2b34732788f5695be1a6bf5b36bb71bcfa3b7fb9R28

which indicates that .reduce() not working was a known limitation. @weswigham – any chance you already have an issue tracking that somewhere, or is this ticket fine? I've also checked some other common array methods and created a new issue to track the ones that aren't working.

@github-actions
Copy link

github-actions bot commented Jun 8, 2023

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

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Unactionable There isn't something we can do with this issue
Projects
None yet
Development

No branches or pull requests

5 participants