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

Inconsistent inferred types when using array methods map() and reduce() #8111

Closed
eidheim opened this issue Oct 2, 2019 · 2 comments
Closed

Comments

@eidheim
Copy link

eidheim commented Oct 2, 2019

From:

// @flow

let v1 = [1, 2, 3];

let v2 = v1.map(e => e);

let sum = v2.reduce((e, acc) => e + acc, 0);

Inferred type of v2 is Array<(any | number)>, and of sum is any | number. One would think the inferred types should instead be Array<(number)> and number respectively.

The above behaviour was introduced in Flow v0.86.

However, the following code infers correct types:

// @flow

let v1 = [1, 2, 3];

let v2 = v1.map(e => e);

let sum = v1.reduce((e, acc) => e + acc, 0);

That is, the inferred types of v2 and sum are Array<number> and number.

Flow version: v0.86-v0.108

Expected behavior

Consistent inferred types of v2 and sum in the above examples.

Actual behavior

Inconsistent inferred types of v2 and sum in the above examples.

Try-Flow

@eidheim eidheim changed the title Inconsistent inferred types when using array methods map() and filter() Inconsistent inferred types when using array methods map() and reduce() Oct 4, 2019
@knu
Copy link

knu commented Aug 31, 2020

This is still an issue in v0.132.0. v2 is Array<number> at the time it is born and adding a piece of code that uses it afterwards turns it to a looser type Array<any | number>, which is quite unexpected.

@eidheim
Copy link
Author

eidheim commented Dec 21, 2020

@eidheim eidheim closed this as completed Dec 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants