{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/11263","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/11263/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/11263/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/11263/events","html_url":"https://github.com/Microsoft/TypeScript/pull/11263","id":180185200,"number":11263,"title":"Control flow analysis for implicit any variables","user":{"login":"ahejlsberg","id":4226954,"avatar_url":"https://avatars.githubusercontent.com/u/4226954?v=3","gravatar_id":"","url":"https://api.github.com/users/ahejlsberg","html_url":"https://github.com/ahejlsberg","followers_url":"https://api.github.com/users/ahejlsberg/followers","following_url":"https://api.github.com/users/ahejlsberg/following{/other_user}","gists_url":"https://api.github.com/users/ahejlsberg/gists{/gist_id}","starred_url":"https://api.github.com/users/ahejlsberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ahejlsberg/subscriptions","organizations_url":"https://api.github.com/users/ahejlsberg/orgs","repos_url":"https://api.github.com/users/ahejlsberg/repos","events_url":"https://api.github.com/users/ahejlsberg/events{/privacy}","received_events_url":"https://api.github.com/users/ahejlsberg/received_events","type":"User","site_admin":false},"labels":[{"id":164593272,"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/cla-not-required","name":"cla-not-required","color":"009800","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-09-29T23:07:48Z","updated_at":"2016-11-09T17:49:18Z","closed_at":"2016-10-06T21:23:38Z","pull_request":{"url":"https://api.github.com/repos/Microsoft/TypeScript/pulls/11263","html_url":"https://github.com/Microsoft/TypeScript/pull/11263","diff_url":"https://github.com/Microsoft/TypeScript/pull/11263.diff","patch_url":"https://github.com/Microsoft/TypeScript/pull/11263.patch"},"body":"This PR introduces control flow analysis for `let` and `var` variables that have no type annotation and either no initial value or an initial value of `null` or `undefined`.\n\n``` ts\nfunction f(cond: boolean) {\n    let x;\n    if (cond) {\n        x = \"hello\";\n        x;  // string\n    }\n    else {\n        x = 123;\n        x;  // number\n    }\n    return x;  // string | number\n}\n\nfunction g(cond: boolean) {\n    let y = null;\n    if (cond) {\n        y = \"hello\";\n    }\n    return y;  // string | null\n}\n```\n\nIn the example above, `x` and `y` implicitly have declared types of `any` but control flow analysis can determine their _actual_ types at every reference. Therefore, no errors are reported even when the example is compiled with `--noImplicitAny`.\n\nControl flow analysis is unable to determine the actual types of implicit `any` variables when they are referenced in nested functions. In such cases, the variables will appear to have type `any` in the nested functions and errors will be reported for the references if `--noImplicitAny` is enabled.\n\n``` ts\nfunction f(cond: boolean) {\n    let x;  // Error: Variable 'x' implicitly has type 'any' in some locations where its type cannot be determined.\n    if (cond) {\n        x = \"hello\";\n    }\n    else {\n        x = 123;\n    }\n    return x;  // type string | number\n    function bar() {\n        const y = x;  // Error: Variable 'x' implicitly has an 'any' type.\n    }\n}\n```\n\nIn time it is possible that control flow analysis will be able to more accurately determine types of references to outer variables from nested functions in _some_ cases, but given that nested functions are first class objects that can be arbitrarily passed around and called, it is effectively impossible to analyze all such scenarios.\n"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/259478069","html_url":"https://github.com/Microsoft/TypeScript/pull/11263#issuecomment-259478069","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/11263","id":259478069,"user":{"login":"sandersn","id":293473,"avatar_url":"https://avatars.githubusercontent.com/u/293473?v=3","gravatar_id":"","url":"https://api.github.com/users/sandersn","html_url":"https://github.com/sandersn","followers_url":"https://api.github.com/users/sandersn/followers","following_url":"https://api.github.com/users/sandersn/following{/other_user}","gists_url":"https://api.github.com/users/sandersn/gists{/gist_id}","starred_url":"https://api.github.com/users/sandersn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sandersn/subscriptions","organizations_url":"https://api.github.com/users/sandersn/orgs","repos_url":"https://api.github.com/users/sandersn/repos","events_url":"https://api.github.com/users/sandersn/events{/privacy}","received_events_url":"https://api.github.com/users/sandersn/received_events","type":"User","site_admin":false},"created_at":"2016-11-09T17:49:18Z","updated_at":"2016-11-09T17:49:18Z","body":"Typescript is already unsafe in this way. You can also write\r\n\r\n```\r\nlet sns: (string | number)[];\r\nf(sns);\r\n```"}}
{"actor":{"display_login":"sandersn"}}