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

Incorrect type inferred in Array reduce function #39259

Closed
jimador opened this issue Jun 25, 2020 · 3 comments
Closed

Incorrect type inferred in Array reduce function #39259

jimador opened this issue Jun 25, 2020 · 3 comments

Comments

@jimador
Copy link

jimador commented Jun 25, 2020

TypeScript Version: 3.9.2/3.8.3/Nightly

Search Terms:

  • array reduce
  • reduce type inference

Code

type SomeParamType = { hello: string; world: string; }

function brokenInference<T>(someArg: SomeParamType): boolean {
  type KeyType = keyof typeof someArg
  const keys = (Object.keys(someArg) as KeyType[])

  return keys.reduce((previousValue, currentValue) => {
      const result = currentValue == 'hello world'
      return previousValue && result
    }, true)
}

function correctInference(someArg: SomeParamType): boolean {
  type KeyType = keyof typeof someArg
  const keys = (Object.keys(someArg) as KeyType[])
  
  return keys.reduce<boolean>((previousValue, currentValue) => {
      const test = someArg[currentValue] === 'hello world'
      return previousValue && test
    }, true)
}

function correctInference1(someArg: SomeParamType): boolean {
  type KeyType = keyof typeof someArg
  const keys = (Object.keys(someArg) as KeyType[])
  const value = keys.reduce((previousValue, currentValue) => {
      const test = someArg[currentValue] === 'hello world'
      return previousValue && test
    }, true)
  return value
}

Expected behavior:
The type of keys.reduce(...) should be inferred as boolean

Actual behavior:
The type of keys.reduce(...) is inferred as keyof T.

Notes
From what I can tell, there are currently 3 definitions for Array.reduce.

reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: readonly T[]) => T): T;
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: readonly T[]) => T, initialValue: T): T;
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: readonly T[]) => U, initialValue: U): U;

When the reduce function is called from const result = (item: T) => keys.reduce TS appears to be inferring the signature of reduce to be reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: readonly T[]) => U, initialValue: U): U;. When the function is inlined, the signature of reduce is inferred to be reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: readonly T[]) => T, initialValue: T): T;.

Question:

Is this signature needed?

reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: readonly T[]) => T, initialValue: T): T

This signature would seem to satisfy those constraints.

reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: readonly T[]) => U, initialValue: U): U

Playground Link:
3.9.2
3.8.3
Nightly

Related Issues:
This issue appears to be similar to #33886, #25454, #33886, #29604

@RyanCavanaugh
Copy link
Member

Please add to #36554. A shorter repro would be ideal, if possible. Thanks!

@jimador
Copy link
Author

jimador commented Jun 25, 2020

Please add to #36554. A shorter repro would be ideal, if possible. Thanks!

Added comment to #36554 and I made the repro shorter.

@RyanCavanaugh
Copy link
Member

Thank you! We'll track progress over there as we collect more use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants