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

Array.filter does not resolve object properties #6322

Closed
Eiryyy opened this issue May 18, 2018 · 2 comments
Closed

Array.filter does not resolve object properties #6322

Eiryyy opened this issue May 18, 2018 · 2 comments

Comments

@Eiryyy
Copy link

Eiryyy commented May 18, 2018

https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAodBjOA7AzgFzAEEAuYgJ3IEMBPAHgG8qyDyBLbAcwBowAjMgH5WHTgF8AfGAC8YANqowYBmGZgA5FDhx1vARqrqwY7ouWqy6vlXK7+lvkZNmVaq1QBed-dgCuMGGNUAF1ULDxCKDImFnx2Lj0hES4xGSk+bRgAUypsGTA4PgArNLB8GgAHLLgoAuKAOj4ZaVl1ZM51MJwCMAAhMiJKWkY1dsSwdsl8onqoNhh8LPIACigASiA

const A: Array<{a: string, b: ?string}> = [
  { a: 'foo', b: 'a' },
  { a: 'bar', b: 'a' },
  { a: 'baz', b: null }
]
const f: { a: string, b: ?string } => boolean = obj => typeof obj.b === 'string'
const B: Array<{a: string, b: string}> = A.filter(f)

errors:

10: const B: Array<{a: string, b: string}> = A.filter(f)                                             ^ Cannot assign `A.filter(...)` to `B` because null or undefined [1] is incompatible with string [2] in property `b` of array element.
References:
4: const A: Array<{a: string, b: ?string}> = [
                                 ^ [1]
10: const B: Array<{a: string, b: string}> = A.filter(f)
                                  ^ [2]
@TrySound
Copy link
Contributor

Refinement for filter doesn't work. It's known. User reduce or flatMap instead where you need refinement.

#1414

@lll000111
Copy link
Contributor

lll000111 commented May 18, 2018

filter works when you use the special construct filter(Boolean) to filter out falsy values like null. So the alternative to using something like a // $FlowIssue comment to disable Flow for a line of code is to first use map to map whatever is to be filtered to null and the remove it using the beforementioned construct, at the cost of an additional loop construct. Actually using boolean values does not work though, it has to be null or undefined for the falsy value (for Flow, the Javascript code would of course be okay with it).

Example (1st example with booleans has errors, second one with null does not)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants