Created
March 31, 2017 02:10
-
-
Save antonycourtney/f27463fee13a0997cee9b66f4584b54e to your computer and use it in GitHub Desktop.
Flow data flow analysis loses info under a lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* @flow */ | |
/* Paste this into https://flow.org/try/ to see the bug: */ | |
const f = (lut: ?{[key: string]: number}): number => { | |
// This correctly notices that we're doing a null check on lut: | |
const testKey = 'fizz' | |
const y = (lut != null) ? lut[testKey] : 0 | |
// Putting the access of lut under a lambda (the arg to map) | |
// loses this info: | |
const keyList = ['foo','fee','foe'] | |
const zs = (lut != null) ? keyList.map(k => lut[k]) : [] | |
return y | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually this may just be Flow being overly conservative --
See:
https://flow.org/en/docs/lang/refinements/
And, indeed, a workaround is to change first two lines to:
and then it all works.