Skip to content

Instantly share code, notes, and snippets.

@antonycourtney
Created March 31, 2017 02:10
Show Gist options
  • Save antonycourtney/f27463fee13a0997cee9b66f4584b54e to your computer and use it in GitHub Desktop.
Save antonycourtney/f27463fee13a0997cee9b66f4584b54e to your computer and use it in GitHub Desktop.
Flow data flow analysis loses info under a lambda
/* @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
}
@antonycourtney
Copy link
Author

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:

const f = (inLut: ?{[key: string]: number}): number => {
  const lut = inLut

and then it all works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment