{"action":"closed","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1883","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1883/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1883/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1883/events","html_url":"https://github.com/Microsoft/TypeScript/issues/1883","id":56222115,"number":1883,"title":"Union type interaction with overloads","user":{"login":"danielearwicker","id":365934,"avatar_url":"https://avatars.githubusercontent.com/u/365934?v=3","gravatar_id":"","url":"https://api.github.com/users/danielearwicker","html_url":"https://github.com/danielearwicker","followers_url":"https://api.github.com/users/danielearwicker/followers","following_url":"https://api.github.com/users/danielearwicker/following{/other_user}","gists_url":"https://api.github.com/users/danielearwicker/gists{/gist_id}","starred_url":"https://api.github.com/users/danielearwicker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danielearwicker/subscriptions","organizations_url":"https://api.github.com/users/danielearwicker/orgs","repos_url":"https://api.github.com/users/danielearwicker/repos","events_url":"https://api.github.com/users/danielearwicker/events{/privacy}","received_events_url":"https://api.github.com/users/danielearwicker/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-02-02T10:58:04Z","updated_at":"2015-02-02T20:40:14Z","closed_at":"2015-02-02T20:40:14Z","body":"Where there are overloads of a method, the method cannot be directly called with a parameter of a union type that matches the available overloads.\r\n\r\nHere's a cut-down example that highlights the apparent issue (can be pasted into TypeScript playground):\r\n\r\n```ts\r\n\tinterface JQuery {\r\n\t\tappend(content1: JQuery): JQuery;\r\n\t\tappend(content1: string): JQuery;\r\n\t}\r\n\t\r\n\tvar messageElement: JQuery;\r\n\t\r\n\tfunction setMessage(message: string | JQuery) {\r\n\r\n\t\tmessageElement.append(message); // error JQuery is not assignable to type string\r\n\r\n\t\t// Help the compiler out with a type guard, it's okay\r\n\t\tif (typeof message === \"string\") {\r\n\t\t\tmessageElement.append(message);\r\n\t\t} else {\r\n\t\t\tmessageElement.append(message);\r\n\t\t}\r\n\t}\r\n```\r\n\r\nIt's clear why the heavily-redundant type-guard workaround makes it easier for the compiler: in the general case each overload is free to return a different type, e.g. hypothetically weird example:\r\n\r\n```ts\r\n\tinterface JQuery {\r\n\t\tappend(content1: JQuery): boolean;\r\n\t\tappend(content1: string): number;\r\n\t}\r\n```\r\n\r\nThis is an example of overloads capturing a specific relation that would be obscured if we instead used union types:\r\n\r\n```ts\r\n\tinterface JQuery {\r\n\t\tappend(content1: JQuery|string): boolean|number;\r\n\t}\r\n```\r\nBy writing it with union types, we don't capture the fact that JQuery goes to boolean, whereas string goes to number, but string never goes to boolean, etc..\r\n\r\nBut what I'm hoping is that compiler could behave as if there was an additional \"catch all\" overload that accepted the union of the parameters and return the union of the return types, i.e.:\r\n```ts\r\n\tinterface JQuery {\r\n\t\tappend(content1: JQuery): boolean;\r\n\t\tappend(content1: string): number;\r\n\r\n                // Therefore the compiler infers the existence of this extra overload:\r\n\t\tappend(content1: JQuery|string): boolean|number;\r\n\t}\r\n```\r\nThat way, union type parameters would go into overloads more smoothly."}}