Skip to content

wrong type inferred in reduce statementΒ #46707

Closed
@ivanhofer

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

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