{"action":"created","issue":{"url":"https://api.github.com/repos/facebook/flow/issues/487","labels_url":"https://api.github.com/repos/facebook/flow/issues/487/labels{/name}","comments_url":"https://api.github.com/repos/facebook/flow/issues/487/comments","events_url":"https://api.github.com/repos/facebook/flow/issues/487/events","html_url":"https://github.com/facebook/flow/issues/487","id":84025948,"number":487,"title":"parseInt default definition doesn't support number inputs","user":{"login":"chrisjohnson","id":178144,"avatar_url":"https://avatars.githubusercontent.com/u/178144?v=3","gravatar_id":"","url":"https://api.github.com/users/chrisjohnson","html_url":"https://github.com/chrisjohnson","followers_url":"https://api.github.com/users/chrisjohnson/followers","following_url":"https://api.github.com/users/chrisjohnson/following{/other_user}","gists_url":"https://api.github.com/users/chrisjohnson/gists{/gist_id}","starred_url":"https://api.github.com/users/chrisjohnson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chrisjohnson/subscriptions","organizations_url":"https://api.github.com/users/chrisjohnson/orgs","repos_url":"https://api.github.com/users/chrisjohnson/repos","events_url":"https://api.github.com/users/chrisjohnson/events{/privacy}","received_events_url":"https://api.github.com/users/chrisjohnson/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-02T13:37:52Z","updated_at":"2015-06-03T02:01:16Z","closed_at":"2015-06-03T02:01:16Z","body":"So I've got a function that looks like this:\r\n\r\n```\r\nfunction foo(numeric) {\r\n    if (numeric == null) {\r\n        numeric = 16;\r\n    }\r\n    ...\r\n    return parseInt(numeric) + something;\r\n}\r\n```\r\n\r\nAnd it complains that numeric isn't a string. Seems like parseInt shouldn't accept *only* strings, but strings and/or numbers (in cases where either/or might be passed in). I tried designating numeric as /*: number */ and string, and I tried number|string (is that valid?) but no dice. It either gets tripped up on the setting to number, comparison to null, or parseInt.\r\n\r\nAm I off base here or is this a bug in the default declaration for parseInt's inputs? I see that it's marked as only accepting a string but why wouldn't it be allowed to accept possibly-numbers?"},"comment":{"url":"https://api.github.com/repos/facebook/flow/issues/comments/108159583","html_url":"https://github.com/facebook/flow/issues/487#issuecomment-108159583","issue_url":"https://api.github.com/repos/facebook/flow/issues/487","id":108159583,"user":{"login":"mroch","id":3012,"avatar_url":"https://avatars.githubusercontent.com/u/3012?v=3","gravatar_id":"","url":"https://api.github.com/users/mroch","html_url":"https://github.com/mroch","followers_url":"https://api.github.com/users/mroch/followers","following_url":"https://api.github.com/users/mroch/following{/other_user}","gists_url":"https://api.github.com/users/mroch/gists{/gist_id}","starred_url":"https://api.github.com/users/mroch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mroch/subscriptions","organizations_url":"https://api.github.com/users/mroch/orgs","repos_url":"https://api.github.com/users/mroch/repos","events_url":"https://api.github.com/users/mroch/events{/privacy}","received_events_url":"https://api.github.com/users/mroch/received_events","type":"User","site_admin":false},"created_at":"2015-06-03T02:01:16Z","updated_at":"2015-06-03T02:01:16Z","body":"https://people.mozilla.org/~jorendorff/es6-draft.html#sec-parseint-string-radix\r\n\r\nif you pass a number to `parseInt`, it gets converted to a string and then parsed back to a number again. generally, flow doesn't allow implicit type coercion, so you have to pass an actual string. I can think of two options:\r\n\r\n- `return parseInt(String(numeric)) + something;` will explicitly coerce `numeric` to a string, which `parseInt` would have done anyway, so it should act the same way at runtime\r\n\r\n- something like this:\r\n\r\n```\r\nfunction foo(numeric: ?(number | string)): number {\r\n  var num = numeric == null ? 16 : (typeof numeric == \"number\" ? numeric : parseInt(numeric));\r\n  return num + something;\r\n}\r\n```\r\n\r\nflow will follow the different branches of that nested ternary and understand that `number` is always a number. note that if you did something like\r\n\r\n```\r\nvar num;\r\nif (numeric == null) {\r\n  num = 16;\r\n} else if (typeof numeric == \"number\") {\r\n  num = numeric;\r\n} else {\r\n  num = parseInt(numeric);\r\n}\r\n```\r\n\r\nthen flow will (incorrectly) think that `num` could still be `undefined` because of its initial state. that's a bug that we're hoping to address soon, so in the meantime a ternary is necessary.\r\n\r\nglad you asked, feel free to reopen."}}