{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8513","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8513/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8513/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8513/events","html_url":"https://github.com/Microsoft/TypeScript/issues/8513","id":153610104,"number":8513,"title":"why does initialization narrow type?","user":{"login":"aleksey-bykov","id":937933,"avatar_url":"https://avatars.githubusercontent.com/u/937933?v=3","gravatar_id":"","url":"https://api.github.com/users/aleksey-bykov","html_url":"https://github.com/aleksey-bykov","followers_url":"https://api.github.com/users/aleksey-bykov/followers","following_url":"https://api.github.com/users/aleksey-bykov/following{/other_user}","gists_url":"https://api.github.com/users/aleksey-bykov/gists{/gist_id}","starred_url":"https://api.github.com/users/aleksey-bykov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aleksey-bykov/subscriptions","organizations_url":"https://api.github.com/users/aleksey-bykov/orgs","repos_url":"https://api.github.com/users/aleksey-bykov/repos","events_url":"https://api.github.com/users/aleksey-bykov/events{/privacy}","received_events_url":"https://api.github.com/users/aleksey-bykov/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":{"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},"milestone":{"url":"https://api.github.com/repos/Microsoft/TypeScript/milestones/1","html_url":"https://github.com/Microsoft/TypeScript/milestones/TypeScript%202.0","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/milestones/1/labels","id":721040,"number":1,"title":"TypeScript 2.0","description":"The next major release of TypeScript","creator":{"login":"RyanCavanaugh","id":6685088,"avatar_url":"https://avatars.githubusercontent.com/u/6685088?v=3","gravatar_id":"","url":"https://api.github.com/users/RyanCavanaugh","html_url":"https://github.com/RyanCavanaugh","followers_url":"https://api.github.com/users/RyanCavanaugh/followers","following_url":"https://api.github.com/users/RyanCavanaugh/following{/other_user}","gists_url":"https://api.github.com/users/RyanCavanaugh/gists{/gist_id}","starred_url":"https://api.github.com/users/RyanCavanaugh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RyanCavanaugh/subscriptions","organizations_url":"https://api.github.com/users/RyanCavanaugh/orgs","repos_url":"https://api.github.com/users/RyanCavanaugh/repos","events_url":"https://api.github.com/users/RyanCavanaugh/events{/privacy}","received_events_url":"https://api.github.com/users/RyanCavanaugh/received_events","type":"User","site_admin":false},"open_issues":126,"closed_issues":274,"state":"open","created_at":"2014-07-14T23:24:03Z","updated_at":"2016-05-08T15:24:43Z","due_on":null,"closed_at":null},"comments":18,"created_at":"2016-05-07T18:27:15Z","updated_at":"2016-05-08T17:07:07Z","closed_at":null,"body":"nightly build\r\n\r\n```\r\n\"compilerOptions\": {\r\n        \"module\": \"amd\",\r\n        \"moduleResolution\": \"classic\",\r\n        \"noEmitOnError\": true,\r\n        \"allowUnreachableCode\": false,\r\n        \"allowUnusedLabels\": false,\r\n        \"outDir\": \"./built\",\r\n        \"noResolve\": true,\r\n        \"noImplicitAny\": true,\r\n        \"noImplicitReturns\": true,\r\n        \"forceConsistentCasingInFileNames\": true,\r\n        \"noFallthroughCasesInSwitch\": true,\r\n        \"target\": \"es5\"\r\n        // \"strictNullChecks\": true\r\n    }\r\n```\r\n\r\n```typescript\r\n\r\nexport type Optional<a> = Some<a> | None;\r\n\r\nexport interface None { readonly none: string; }\r\nexport interface Some<a> { readonly some: a; }\r\n\r\nexport const none : None = { none: '' };\r\n\r\nexport function isSome<a>(value: Optional<a>): value is Some<a> {\r\n    return 'some' in value;\r\n}\r\n\r\nexport function fn<r>(): void {\r\n    let result: Optional<r> = none;\r\n    isSome(result)\r\n        ? result.some\r\n        : undefined\r\n}\r\n\r\n```\r\n\r\n\r\n![image](https://cloud.githubusercontent.com/assets/937933/15093796/c6856372-145f-11e6-9588-806c34035fb0.png)\r\n"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/217733352","html_url":"https://github.com/Microsoft/TypeScript/issues/8513#issuecomment-217733352","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8513","id":217733352,"user":{"login":"ivogabe","id":4706418,"avatar_url":"https://avatars.githubusercontent.com/u/4706418?v=3","gravatar_id":"","url":"https://api.github.com/users/ivogabe","html_url":"https://github.com/ivogabe","followers_url":"https://api.github.com/users/ivogabe/followers","following_url":"https://api.github.com/users/ivogabe/following{/other_user}","gists_url":"https://api.github.com/users/ivogabe/gists{/gist_id}","starred_url":"https://api.github.com/users/ivogabe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ivogabe/subscriptions","organizations_url":"https://api.github.com/users/ivogabe/orgs","repos_url":"https://api.github.com/users/ivogabe/repos","events_url":"https://api.github.com/users/ivogabe/events{/privacy}","received_events_url":"https://api.github.com/users/ivogabe/received_events","type":"User","site_admin":false},"created_at":"2016-05-08T17:07:06Z","updated_at":"2016-05-08T17:07:06Z","body":"@myitcv I think that the best use case would be the following:\r\n```typescript\r\nfunction test(x: string | string[]) {\r\n  if (typeof x === \"string\") x = [x];\r\n  // x: string[]\r\n}\r\n```\r\nThis narrowing is not necessary for control flow analysis, but you can do this with no extra effort. It should, aside from this issue, give more accurate types so I don't a big problem with it. I've used it a lot lately and I even didn't need type casts any more.\r\n\r\nWhen non union types are narrowed, you can get unexpected behavior in various cases, for instance:\r\n```typescript\r\nlet x: (string | number)[];\r\nx = [42]; // x: number[]\r\nx.push(\"\"); // Would be an error if x was narrowed\r\n\r\nlet y: { z?: boolean };\r\ny = {};\r\n// y: {}\r\ny.z = true; // Error: z does not exist on {}\r\n```"}}