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

wrong type inferred in reduce statement #46707

Closed
ivanhofer opened this issue Nov 6, 2021 · 4 comments
Closed

wrong type inferred in reduce statement #46707

ivanhofer opened this issue Nov 6, 2021 · 4 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@ivanhofer
Copy link

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

@ahejlsberg
Copy link
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.

@ahejlsberg ahejlsberg added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Nov 8, 2021
@andrewbranch
Copy link
Member

Duplicate of #46438, see also #36554

@ivanhofer
Copy link
Author

Thanks @ahejlsberg for the good explanation and the workaround!

@jcalz
Copy link
Contributor

jcalz commented Oct 9, 2022

Fixed by #48380, it seems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

5 participants