{"action":"created","issue":{"url":"https://api.github.com/repos/facebook/flow/issues/4880","repository_url":"https://api.github.com/repos/facebook/flow","labels_url":"https://api.github.com/repos/facebook/flow/issues/4880/labels{/name}","comments_url":"https://api.github.com/repos/facebook/flow/issues/4880/comments","events_url":"https://api.github.com/repos/facebook/flow/issues/4880/events","html_url":"https://github.com/facebook/flow/issues/4880","id":257547173,"number":4880,"title":"Flow does not allow Map.has() to refine type ","user":{"login":"bradennapier","id":15365418,"avatar_url":"https://avatars3.githubusercontent.com/u/15365418?v=4","gravatar_id":"","url":"https://api.github.com/users/bradennapier","html_url":"https://github.com/bradennapier","followers_url":"https://api.github.com/users/bradennapier/followers","following_url":"https://api.github.com/users/bradennapier/following{/other_user}","gists_url":"https://api.github.com/users/bradennapier/gists{/gist_id}","starred_url":"https://api.github.com/users/bradennapier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bradennapier/subscriptions","organizations_url":"https://api.github.com/users/bradennapier/orgs","repos_url":"https://api.github.com/users/bradennapier/repos","events_url":"https://api.github.com/users/bradennapier/events{/privacy}","received_events_url":"https://api.github.com/users/bradennapier/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-09-13T23:06:01Z","updated_at":"2018-03-09T11:39:27Z","closed_at":null,"author_association":"NONE","body":"```js\r\ntype MapValue = {|\r\n                  \tone: string,\r\n                 \ttwo: number\r\n              \t|}\r\n\r\nconst MyMap: Map<string, MapValue> = new Map()\r\n\r\nfunction test(id: string): number | MapValue {\r\n  if (MyMap.has(id)) {\r\n  \treturn MyMap.get(id)\r\n  }\r\n  return 1\r\n}\r\n```\r\n\r\n```\r\n12:   \treturn MyMap.get(id)              ^ undefined. This type is incompatible with the expected return type of\r\n10: function test(id: string): number | MapValue {\r\n                               ^ union: number | MapValue\r\n```\r\n\r\nhttps://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgCyAhjgGrEwCuBAvGAN4A+qYb7Hn7AkHAHZ4AXGADOGAE4BLPgHMANKy5KeGBHGF8qAWwBGecYuUduTAL7oAxvzFEsJHMPsAeMVNlyipCtTwA+MPQCSPYAFACU6FBUfBYYkvxgGHhiIZIAJsKu0jJhGtp64mBMnuSUNIyKklBgIYR2pAB0ABbEIqlpYWEVbNzieBhU4ny29g0y-e0RbOZsfQNDYACMqOZAA\r\n"},"comment":{"url":"https://api.github.com/repos/facebook/flow/issues/comments/371789983","html_url":"https://github.com/facebook/flow/issues/4880#issuecomment-371789983","issue_url":"https://api.github.com/repos/facebook/flow/issues/4880","id":371789983,"user":{"login":"johngeorgewright","id":1423566,"avatar_url":"https://avatars2.githubusercontent.com/u/1423566?v=4","gravatar_id":"","url":"https://api.github.com/users/johngeorgewright","html_url":"https://github.com/johngeorgewright","followers_url":"https://api.github.com/users/johngeorgewright/followers","following_url":"https://api.github.com/users/johngeorgewright/following{/other_user}","gists_url":"https://api.github.com/users/johngeorgewright/gists{/gist_id}","starred_url":"https://api.github.com/users/johngeorgewright/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johngeorgewright/subscriptions","organizations_url":"https://api.github.com/users/johngeorgewright/orgs","repos_url":"https://api.github.com/users/johngeorgewright/repos","events_url":"https://api.github.com/users/johngeorgewright/events{/privacy}","received_events_url":"https://api.github.com/users/johngeorgewright/received_events","type":"User","site_admin":false},"created_at":"2018-03-09T11:39:27Z","updated_at":"2018-03-09T11:39:27Z","author_association":"NONE","body":"I have a [similar problem](https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgBCcAHmALxgDOGATgJYB2A5prgQCoAWTzF1dXm3xhuvKvwDKeDAB4xLAHzoAxjACGVCQEEMGeirABvVGDAAjUnioAuMAFl1OWcRIAaUTxZVlZs6bBaPABbOAA3PAU+AApLEjtXDwwvZjsogEo7SzgYPHVGYwCzIIwAV1oC5PoqADo46xquTVjSdLAAMnawKtr62uYZFpJ0moATPFyMPGiqlnSAgF9UJaA).\r\n\r\n```typescript\r\ntype Box = string\r\ntype Thing = string\r\ntype Things = Set<Thing>\r\n\r\nclass Attic {\r\n  boxes: Map<Box, Things>\r\n    \r\n  removeThing (box: Box, thing: Thing): boolean {\r\n    return this.boxes.has(box) && this.boxes.get(box).delete(thing)\r\n  }\r\n}\r\n```\r\n\r\n```\r\n11:     return this.boxes.has(box) && this.boxes.get(box).delete(thing)\r\n                                                          ^ Cannot call `this.boxes.get(...).delete` because property `delete` is missing in undefined [1].\r\nReferences:\r\n[LIB] static/v0.67.1/flowlib/core.js:561:     get(key: K): V | void;\r\n                                                               ^ [1]\r\n```\r\n\r\nI had to [rewrite it](https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgBCcAHmALxgDOGATgJYB2A5prgQCoAWTzF1dXm3xhuvKvwDKeDAB4xLAHzoAxjACGVCQEEMGeirABvVGDAAjUnioAuMAFl1OWcRIAaUTxZVlZs6bBaPABbOAA3PAU+AApLEjtXDwwvZjsogEo7SzgYPHVGYwCzFThGGjBk8X5KqgA6OOta5hlY0nSiwJkAV1oC7Hw4KAqUiQBCckoAci7GABM8KCY8WcmwADI14fFa+dyMPGjKlnazAF9Uc6A) as @lll000111 suggests:\r\n\r\n```typescript\r\ntype Box = string\r\ntype Thing = string\r\ntype Things = Set<Thing>\r\n\r\nclass Attic {\r\n  boxes: Map<Box, Things>\r\n    \r\n  removeThing (box: Box, thing: Thing): boolean {\r\n    const things = this.boxes.get(box)\r\n    return typeof things !== 'undefined' && things.delete(thing)\r\n  }\r\n}\r\n```\r\n\r\nSo, you can get around it, but it's less than ideal."}}
{"actor":{"display_login":"johngeorgewright"}}