{"action":"opened","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/24929","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/24929/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/24929/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/24929/events","html_url":"https://github.com/Microsoft/TypeScript/issues/24929","id":332087943,"node_id":"MDU6SXNzdWUzMzIwODc5NDM=","number":24929,"title":"Use conditional type for return type to narrow function with union parameter type ","user":{"login":"aneilbaboo","id":2592723,"node_id":"MDQ6VXNlcjI1OTI3MjM=","avatar_url":"https://avatars1.githubusercontent.com/u/2592723?v=4","gravatar_id":"","url":"https://api.github.com/users/aneilbaboo","html_url":"https://github.com/aneilbaboo","followers_url":"https://api.github.com/users/aneilbaboo/followers","following_url":"https://api.github.com/users/aneilbaboo/following{/other_user}","gists_url":"https://api.github.com/users/aneilbaboo/gists{/gist_id}","starred_url":"https://api.github.com/users/aneilbaboo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aneilbaboo/subscriptions","organizations_url":"https://api.github.com/users/aneilbaboo/orgs","repos_url":"https://api.github.com/users/aneilbaboo/repos","events_url":"https://api.github.com/users/aneilbaboo/events{/privacy}","received_events_url":"https://api.github.com/users/aneilbaboo/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-06-13T17:17:37Z","updated_at":"2018-06-13T17:17:37Z","closed_at":null,"author_association":"NONE","body":"## Search Terms\r\n\r\nconditional return type narrowing generics\r\n\r\n## Suggestion\r\n\r\nIt seems like it should be possible to constrain an argument type with a union and use a conditional expression to provide a narrower return type:\r\n\r\nFor example, \r\n```typescript\r\nexport function foo<T extends string|number>(val: T): T extends string ? string : number {\r\n  return val;\r\n} \r\n// expected this to work, but received:\r\n// Type 'T' is not assignable to type 'T extends string ? string : number'.\r\n//  Type 'string | number' is not assignable to type 'T extends string ? string : number'.\r\n//    Type 'string' is not assignable to type 'T extends string ? string : number'.\r\n```\r\n## Use Cases\r\n\r\nA common use case is a function which can process single objects or arrays of objects, and returns single objects or an array, respectively.\r\n\r\n## Examples\r\n\r\n```typescript\r\nfunction capitalize<T extends string | string[]>(\r\n  input: T\r\n): T extends string[] ? string[] : string {\r\n  if (isString(input)) {\r\n    return input[0].toUpperCase() + input.slice(1);\r\n  } else {\r\n\treturn input.map(elt => capitalize(elt));\r\n  }\r\n}\r\n```\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n* [x] This wouldn't be a breaking change in existing TypeScript / JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. new expression-level syntax)\r\n\r\n"}}
{"actor":{"display_login":"aneilbaboo"}}