Skip to content

Array and IteratorObject reduce method type error #60204

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

Closed
2chanhaeng opened this issue Oct 12, 2024 · 2 comments
Closed

Array and IteratorObject reduce method type error #60204

2chanhaeng opened this issue Oct 12, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@2chanhaeng
Copy link

πŸ”Ž Search Terms

"Array IteratorObject reduce"

πŸ•— Version & Regression Information

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

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99&ts=5.6.3#code/GYVwdgxgLglg9mABAJwKYBMQVQHgCoA0iAygHwAUAsAFCKLABci5MYMsAhgDZPFEQhkaMFCZ4AlIgC8pEgRp1W7XvNqJ2qZGIDaAXUQAfRAEkomjlDjIA8gCMAVqmj5SqyQG8FiAPTf1wZgBBIQ4ATwA6GABnYOQwljNkcUk0KEEkDWRwtExscmAiJShxAG5EL1T09UTsjCxUfMK2YpKaAF8aGlBIWAQAKhQOMABzBo8vUJhULnREAEZWtUnp2YAmRbplmcQAZkWO6hoIBCi4LlRwrjhh8hz68nIOIltJGUQORABqRFsiAAYiHERmNkotjmBTudLtdbnU8o9nq9ZB9vr9EAAidGAoajcjJUpAA

πŸ’» Code

function reduce<T, S>(
  f: (initial: S, current: T) => S,
  init: S,
  iter: T[] | IteratorObject<T>,
) {
  // if (Array.isArray(iter)) return iter.reduce(f, init); 
  return iter.reduce(f, init);
}

function* range() {
  yield 1;
  yield 2;
  yield 3;
}

console.log(reduce((a, b) => a + b, 0, range()));
console.log(reduce((a, b) => a + b, "", range()));

πŸ™ Actual behavior

The type error that should not occur appears at f in the line return iter.reduce(f, init); as follows:

Argument of type '(initial: S, current: T) => S' is not assignable to parameter of type '(previousValue: T, currentValue: T, currentIndex: number) => T'.
  Types of parameters 'initial' and 'previousValue' are incompatible.
    Type 'T' is not assignable to type 'S'.
      'S' could be instantiated with an arbitrary type which could be unrelated to 'T'.

πŸ™‚ Expected behavior

Both T[] and IteratorObject<T> have a reduce method, and the interfaces of the two methods are identical. Therefore, the error should not occur. For example, the type error disappears if the commented line is uncommented. All that line does is distinguish whether iter is a T[] or an IteratorObject<T>. In fact, the code after this conditional branch is identical. Therefore, this error should not exist.

Additional information about the issue

No response

@2chanhaeng 2chanhaeng changed the title Array and IteratorObject reduce method type error Array and IteratorObject reduce method type error Oct 12, 2024
@jcalz
Copy link
Contributor

jcalz commented Oct 12, 2024

Feels like another #60006... unions of callables are not always callable (e.g., overloads and generics)

Personally I'd just work around it by manually widening to whatever callable type you think it should be in the first place, something like

function reduce<T, S>(
  f: (initial: S, current: T) => S,
  init: S,
  iter: T[] | IteratorObject<T>,
) {
  const i: {
    reduce<S>(callbackfn: (previousValue: S, currentValue: T) => S, initialValue: S): S
  } = iter;
  return i.reduce(f, init);
}

Playground

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 16, 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 Oct 19, 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