Title: Infer return type of a function with provided argument types · Issue #26043 · microsoft/TypeScript · GitHub
Open Graph Title: Infer return type of a function with provided argument types · Issue #26043 · microsoft/TypeScript
X Title: Infer return type of a function with provided argument types · Issue #26043 · microsoft/TypeScript
Description: I'm trying to create something similar to native ReturnType type. In comparison to the later, it should also consider generic types of arguments. I guess the idea will be clear from the code below. If it can be done now - I'd be happy to...
Open Graph Description: I'm trying to create something similar to native ReturnType type. In comparison to the later, it should also consider generic types of arguments. I guess the idea will be clear from the code below....
X Description: I'm trying to create something similar to native ReturnType type. In comparison to the later, it should also consider generic types of arguments. I guess the idea will be clear from the code be...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/26043
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Infer return type of a function with provided argument types","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\nI'm trying to create something similar to native ReturnType type. In comparison to the later, it should also consider generic types of arguments.\r\n\r\nI guess the idea will be clear from the code below.\r\n\r\nIf it can be done now - I'd be happy to receive some links. Otherways it'd be great to know if there are any plans to cover that behavior. In case it could be achieved by another not-implemented feature I'll be glad for the link to an issue to be able to track its progress. \r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 2.9.2\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** infer function return type\r\n\r\n**Code**\r\n\r\n```ts\r\n// it works with actual code:\r\nfunction identity\u003cT>(a: T) {\r\n return a;\r\n}\r\n\r\nconst one = identity(1); // typeof one === 1 # everything is great\r\n\r\nfunction call\u003cA, R>(arg: A, fn: Func\u003cA, R>): R {\r\n return fn(arg);\r\n}\r\n\r\nconst two = call(2 as 2, i => i) // typeof two === 2 # everything is great\r\n\r\n// but won't work with types\r\ntype Func\u003cA, R> = (arg: A) => R;\r\n// ReturnTypeByArg is inspired by `ReturnType` from TS 2.8.*\r\ntype ReturnTypeByArg\u003cT, F extends Func\u003cT, any>> = F extends Func\u003cT, infer R> ? R : any;\r\n\r\ntype Identity = \u003cT>(a: T) => T;\r\ntype Result = ReturnTypeByArg\u003c4, Identity>; // Result === {} # not what I expect\r\n```\r\n\r\n**Expected behavior:**\r\nI expect TS to be able to infer such return type\r\n\r\n**Actual behavior:**\r\nReturn type is `{}`\r\n\r\n**Playground Link:** [link](https://www.typescriptlang.org/play/#src=%0D%0Afunction%20identity%3CT%3E(a%3A%20T)%20%7B%0D%0A%20%20%20%20return%20a%3B%0D%0A%7D%0D%0A%0D%0Aconst%20one%20%3D%20identity(1)%3B%20%2F%2F%20typeof%20one%20%3D%3D%3D%201%20%23%20everything%20is%20great%0D%0A%0D%0Afunction%20call%3CA%2C%20R%3E(arg%3A%20A%2C%20fn%3A%20Func%3CA%2C%20R%3E)%3A%20R%20%7B%0D%0A%20%20%20%20return%20fn(arg)%3B%0D%0A%7D%0D%0A%0D%0Aconst%20two%20%3D%20call(2%20as%202%2C%20i%20%3D%3E%20i)%20%2F%2F%20typeof%20two%20%3D%3D%3D%202%20%23%20everything%20is%20great%0D%0A%0D%0Atype%20Func%3CA%2C%20R%3E%20%3D%20(arg%3A%20A)%20%3D%3E%20R%3B%0D%0Atype%20ReturnTypeByArg%3CT%2C%20F%20extends%20Func%3CT%2C%20any%3E%3E%20%3D%20F%20extends%20Func%3CT%2C%20infer%20R%3E%20%3F%20R%20%3A%20any%3B%0D%0A%0D%0Atype%20Identity%20%3D%20%3CT%3E(a%3A%20T)%20%3D%3E%20T%3B%0D%0Atype%20Result%20%3D%20ReturnTypeByArg%3C4%2C%20Identity%3E%3B%20%2F%2F%20Result%20%3D%3D%3D%20%7B%7D%20%23%20not%20what%20I%20expect)\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\n","author":{"url":"https://github.com/int0h","@type":"Person","name":"int0h"},"datePublished":"2018-07-28T18:42:01Z","url":"https://github.com/microsoft/TypeScript/issues/26043"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3be13b8e-d712-bee1-e691-f86b966c890a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AAC6:1CCECA:8C5CF5:BBAA6F:6B01BDCD |
| html-safe-nonce | 9b231c946da54210ab631a9eb59984bd12b62d842d044e1f8fb508d1a5b73b6d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQUM2OjFDQ0VDQTo4QzVDRjU6QkJBQTZGOjZCMDFCRENEIiwidmlzaXRvcl9pZCI6IjcwNTgwMTMwMTkyMDk1NTg1MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e59de0643e4468fea58a3e9b9de93c1cd04fcce9a58222f9544ee60ef7dcfbef |
| hovercard-subject-tag | issue:345470038 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/26043/issue_layout |
| twitter:image | https://opengraph.githubassets.com/271f4e20359c526a686a7477052bde93397162df74dc02c4bc68c1a729f325e6/microsoft/TypeScript/issues/26043 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/271f4e20359c526a686a7477052bde93397162df74dc02c4bc68c1a729f325e6/microsoft/TypeScript/issues/26043 |
| og:image:alt | I'm trying to create something similar to native ReturnType type. In comparison to the later, it should also consider generic types of arguments. I guess the idea will be clear from the code below.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | int0h |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width