Skip to content

Type inference too narrow in Array.reduce with strictFunctionTypes on #30821

@jakeboone02

Description

@jakeboone02

TypeScript Version: 3.4.1

Search terms: reduce strictFunctionTypes

Code

When the type of passing is explicitly set to boolean, this fails:

const passing: boolean = [1].reduce(pv => true, true);

Expected behavior:

Type inferred from true as the initialValue argument should be boolean.

Actual behavior:

Type 'boolean' is not assignable to type 'true'. <-- error on the return value of the reducer function. It's as if I wrote this:

const passing = [1].reduce(pv => true, true as true);

The workaround I'm using is this:

const passing: boolean = [1].reduce(pv => true, !!true);

This also works:

const passing: boolean = [1].reduce(pv => true, true as boolean);

Playground link:

Turn on strictFunctionTypes to see the error:

https://www.typescriptlang.org/play/#src=const%20passing%3A%20boolean%20%3D%20%5B1%5D.reduce(pv%20%3D%3E%20true%2C%20true)%3B

Related issues: none found

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions