Skip to content

wrong type inferred in reduce statement #46707

Closed

Description

@ivanhofer

Bug Report

🕗 Version & Regression Information

Tried in 4.4.4 and Nightly version

⏯ Playground Link

https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBAYwgOwM7wJaoEIQgG2AENk4BeOACgDcj8BXYALjnuQGtkIB3ZASha0GwOFjgAjPIRLkAfHBgBPMMAgAzOEMbkyFAOSSCxZHoDcAKHNI08eqmABBACZEwMYFHJVUi5AgAiwCrITsB+GMCoLOhQGMgA5gDaALoANHBEPn6BwaHhkdEwsQkpcAA+ElLGfHJUoblhCBFRcDFxSckCcImG0sjpvcbJtQDe5nATiCjorPYOWQheWLhGJJSZvgFBYXlNkXzjk0cA-BkLOTuNzYdHEywb2dshV5EAdFDATvQIwJSUYB9qOl6jUyPIAcBqOUKvVLvlUK84ggGKFUJQQek1HR7AcblMbLMRBQ7I4FtDWpsLs94e9Pt9fv9AcCgqDwYDybDqXsEUiUZF0SzMdjgLijh8YPQoKREiT0iT5ptkuYAL6WKzTWxzFxuDwAJi8lHOT12zUKxQ66QeWwa8LN7VKFUGJFZdWNLxabRKnRYPSqJAGfuQwzBcDGR2sM3lZIoy0D6yNNu5B1uk1OVqpJsieKO9wTcO5tK+Pz+EKBcBBtVLHLdNN59FRArAfCF+BxlnDGsJXjzXOahfpJaZ5ZZlfZZRhNYLdYbGLgWNbIvJUc27cm4sl0tlhIVfiVqqAA

💻 Code

export const isBoolean = (value: unknown): value is boolean => typeof value === 'boolean';

const useAdapter = (syncDependencies: string[], asyncDependencies: string[] | boolean) => (dependencies: string[]): [boolean, boolean] => {
    const useAsync = isBoolean(asyncDependencies)
        ? asyncDependencies
        : asyncDependencies.reduce((prev, dep) => prev || dependencies.includes(dep), false)

    const use = useAsync || syncDependencies.reduce((prev, dep) => prev || dependencies.includes(dep), false)

    return [use, useAsync]
}

🙁 Actual behavior

TypeScript thinks that syncDependencies.reduce() returns a string. If i remove useAsync || in front of the reduce statement or place it after the reduce statement || useAsync it is detected as a boolean

🙂 Expected behavior

The reduce statement should return a value of type boolean because the value actually is a boolean

Activity

  1. ahejlsberg commented on Nov 8, 2021

    @ahejlsberg
    Member

    This is a known design limitation in our type inference algorithm. See here. Basically the issue in the failing repros is that the false argument is contextually typed by boolean, which causes us to preserve the literal type false. We then use that type as the inferred type for prev, which locks the inference, at which point we can't infer the wider type boolean returned by the arrow function. It's unfortunate, but at this point we don't have a better solution. The workaround is to write false as boolean instead just false.

  2. andrewbranch commented on Nov 8, 2021

    @andrewbranch
    Member

    Duplicate of #46438, see also #36554

  3. ivanhofer commented on Nov 9, 2021

    @ivanhofer
    Author

    Thanks Anders Hejlsberg (@ahejlsberg) for the good explanation and the workaround!

  4. jcalz commented on Oct 9, 2022

    @jcalz
    Contributor

    Fixed by #48380, it seems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions