{"action":"opened","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/15095","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/15095/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/15095/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/15095/events","html_url":"https://github.com/Microsoft/TypeScript/issues/15095","id":220530882,"number":15095,"title":"Generics and type inference","user":{"login":"Roam-Cooper","id":6518745,"avatar_url":"https://avatars1.githubusercontent.com/u/6518745?v=3","gravatar_id":"","url":"https://api.github.com/users/Roam-Cooper","html_url":"https://github.com/Roam-Cooper","followers_url":"https://api.github.com/users/Roam-Cooper/followers","following_url":"https://api.github.com/users/Roam-Cooper/following{/other_user}","gists_url":"https://api.github.com/users/Roam-Cooper/gists{/gist_id}","starred_url":"https://api.github.com/users/Roam-Cooper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Roam-Cooper/subscriptions","organizations_url":"https://api.github.com/users/Roam-Cooper/orgs","repos_url":"https://api.github.com/users/Roam-Cooper/repos","events_url":"https://api.github.com/users/Roam-Cooper/events{/privacy}","received_events_url":"https://api.github.com/users/Roam-Cooper/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-04-10T03:47:26Z","updated_at":"2017-04-10T03:47:26Z","closed_at":null,"body":"<!-- BUGS: Please use this template. -->\r\n<!-- QUESTIONS: This is not a general support forum! Ask Qs at http://stackoverflow.com/questions/tagged/typescript -->\r\n<!-- SUGGESTIONS: See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md -->\r\n\r\n**TypeScript Version:**  2.2.2 / nightly (2.2.0-dev.201xxxxx)\r\n\r\n**Code**\r\n\r\n```ts\r\nexport class Thing<T>{\r\n    constructor(input: Fruit<T>, mapping: () => T) { }\r\n}\r\n\r\nnew Thing(Fruit<apple>null, i=>{\r\nreturn {};\r\n});\r\n```\r\n\r\n**Expected behavior:**\r\nCompiler complains that {} is not assignable to Fruit<apple> as the compiler sees U as a Fruit<apple> FIRST.\r\n**Actual behavior:**\r\nCompiler finds best common type between Fruit<apple> and {} for T which ends up being {}. However, this means that the mapping function is not forced to return Fruit<apple>.\r\n\r\nAdditionally, if I specify mapping as:\r\n```ts\r\nmapping: () => T & {}\r\n```\r\nThen it works, and the mapping function must return a structure meeting the shape of Fruit<apple>.\r\n\r\nBut if the generic type is explicitly provided like so\r\n```ts\r\nnew Thing<apple>(Fruit<apple>null, ()=>{\r\nreturn {};\r\n});\r\n```\r\nThen the above fix no longer works, and it goes back to accepting {} as the return value of the mapping function.\r\n\r\nIs this a problem related to the compiler picking the best common type/type inference for the generic, or am I wrong? (Also, sorry for opening so many issues recently, I'm building something new for work...)"}}
{"actor":{"display_login":"Roam-Cooper"}}