{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9757","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9757/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9757/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9757/events","html_url":"https://github.com/Microsoft/TypeScript/issues/9757","id":165770401,"number":9757,"title":"Aggressive \"used before assigned\" with strictNullChecks","user":{"login":"kitsonk","id":1282577,"avatar_url":"https://avatars.githubusercontent.com/u/1282577?v=3","gravatar_id":"","url":"https://api.github.com/users/kitsonk","html_url":"https://github.com/kitsonk","followers_url":"https://api.github.com/users/kitsonk/followers","following_url":"https://api.github.com/users/kitsonk/following{/other_user}","gists_url":"https://api.github.com/users/kitsonk/gists{/gist_id}","starred_url":"https://api.github.com/users/kitsonk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kitsonk/subscriptions","organizations_url":"https://api.github.com/users/kitsonk/orgs","repos_url":"https://api.github.com/users/kitsonk/repos","events_url":"https://api.github.com/users/kitsonk/events{/privacy}","received_events_url":"https://api.github.com/users/kitsonk/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Bug","name":"Bug","color":"e11d21"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2016-07-15T11:49:20Z","updated_at":"2016-07-17T01:22:34Z","closed_at":null,"body":"**TypeScript Version:**  2.0.0 (beta)\r\n\r\n**Code**\r\n\r\n```ts\r\n// @strictNullChecks: true\r\n\r\nfunction on(event: string, callback: () => void): { destroy(): void; } {\r\n\treturn {\r\n\t\tdestroy() {\r\n\t\t\t/* nothing */\r\n\t\t}\r\n\t};\r\n}\r\n\r\nconst handle = on('example', () => {\r\n\thandle.destroy(); // error TS2454: Variable 'handle' is used before being assigned.\r\n});\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nCompile without errors.\r\n\r\n**Actual behavior:** \r\n\r\nError that `handle` is being used before it is assigned.\r\n\r\nIt seems to be overly aggressive behaviour which means using block scoped variables in a callback is causing usability issues.  Shouldn't flow control assumed that functions would be resolved after the function returns a value?\r\n\r\nSeems to be a little bit related to #9382, but in this case, the type is resolvable (and `handle.destroy()` provides insight.  I also noticed that in VSCode, while other compiler errors are displaying, this one isn't getting flagged in the IDE (that may still just be my setup though)."},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/233159864","html_url":"https://github.com/Microsoft/TypeScript/issues/9757#issuecomment-233159864","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/9757","id":233159864,"user":{"login":"sledorze","id":416760,"avatar_url":"https://avatars.githubusercontent.com/u/416760?v=3","gravatar_id":"","url":"https://api.github.com/users/sledorze","html_url":"https://github.com/sledorze","followers_url":"https://api.github.com/users/sledorze/followers","following_url":"https://api.github.com/users/sledorze/following{/other_user}","gists_url":"https://api.github.com/users/sledorze/gists{/gist_id}","starred_url":"https://api.github.com/users/sledorze/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sledorze/subscriptions","organizations_url":"https://api.github.com/users/sledorze/orgs","repos_url":"https://api.github.com/users/sledorze/repos","events_url":"https://api.github.com/users/sledorze/events{/privacy}","received_events_url":"https://api.github.com/users/sledorze/received_events","type":"User","site_admin":false},"created_at":"2016-07-17T01:22:34Z","updated_at":"2016-07-17T01:22:34Z","body":"I think it should be an error and the user to write defensive code.\r\nUsing some syntactic sugar would help however.. (Feature not implemented yet - not been accepted yet as a next javascript feature so..)\r\n\r\nconst handle = on('example', () => {\r\n    handle?.destroy();\r\n});\r\n\r\nThe type check path to solve this would involve tracking effects, I'm not sure this is within the scope of typescript.\r\n"}}
{"actor":{"display_login":"sledorze"}}