Title: Add type operators that can answer "given function type T, what is the type of its return value when arguments P are passed in?" · Issue #40179 · microsoft/TypeScript · GitHub
Open Graph Title: Add type operators that can answer "given function type T, what is the type of its return value when arguments P are passed in?" · Issue #40179 · microsoft/TypeScript
X Title: Add type operators that can answer "given function type T, what is the type of its return value when arguments P are passed in?" · Issue #40179 · microsoft/TypeScript
Description: Search Terms overload infer function operator arguments parameters return Suggestion // Already possible type ValueForKey Open Graph Description: Search Terms overload infer function operator arguments parameters return Suggestion // Already possible type ValueForKey X Description: Search Terms overload infer function operator arguments parameters return Suggestion // Already possible type ValueForKey<T, K extends keyof T> = T[K]; // Proposal type Callable = (...args: a...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/40179
X: @github
Domain: github.com
Links:
Viewport: width=device-width
Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add type operators that can answer \"given function type T, what is the type of its return value when arguments P are passed in?\"","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.\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, especially the \"Common Feature Requests\" section: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\n--\u003e\r\n\r\n## Search Terms\r\n\r\n\u003c!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily --\u003e\r\noverload infer function operator arguments parameters return\r\n\r\n## Suggestion\r\n\r\n```ts\r\n// Already possible\r\ntype ValueForKey\u003cT, K extends keyof T\u003e = T[K];\r\n\r\n// Proposal\r\ntype Callable = (...args: any[]) =\u003e any;\r\ntype ValueForArguments\u003cT extends Callable, A extends paramsof T\u003e = T(...A);\r\n```\r\n\r\n## Use Cases\r\n\r\nThis is a proposal that would help with overloaded function issues like #26591 and would help my [type testing library](https://humeris.org/espresso-shot) be able to support checks like `expectTypeOf(fn).toBeCallableWith(args)`.\r\n\r\nNote that though `ValueForKey` could be implemented as:\r\n```ts\r\ntype ValueForKey\u003cT, K extends keyof T\u003e = \r\n T extends {[Key in K]: infer U} ? U : never;\r\n```\r\nThe following will only work for non-overloaded functions:\r\n```ts\r\ntype ValueForArguments\u003cT extends Callable, P extends Parameters\u003cT\u003e\u003e = \r\n T extends (...args: P) =\u003e infer R ? R : never;\r\n```\r\nsince both the inference from `Parameters\u003cT\u003e` and the type definition will only consider one of the overloads.\r\n\r\nWhat I'm asking is not for inference to consider multiple overloads, which would be nice but might complexify the implementation of type inference, but direct support for `paramsof` and `T(A1, A2, ...A)` type syntax which behaves correctly with overloaded functions.\r\n\r\n## Examples\r\n```ts\r\ninterface OverloadedFunction {\r\n (arg: number): number;\r\n (arg: string): string;\r\n}\r\n\r\n// `number`\r\ntype Result1 = OverloadedFunction(number);\r\n\r\n// `number | string`\r\ntype Result2 = OverloadedFunction(number | string);\r\n\r\n// `[number] | [string]`\r\ntype Result3 = paramsof OverloadedFunction;\r\n\r\n// `number | string`\r\ntype Result4 = OverloadedFunction(...paramsof OverloadedFunction)\r\n```\r\n\r\n\u003c!--\r\nWhat do you want to use this for?\r\nWhat shortcomings exist with current approaches?\r\n--\u003e\r\n\r\n\r\n\r\n\u003c!-- Show how this would be used and what the behavior would be --\u003e\r\n\r\n## Also\r\nGiven the above, it would also make sense to have something like\r\n```ts\r\ntype Newable = new (...args: any[]) =\u003e any;\r\ntype ValueForArguments\u003cT extends Newable, A extends paramsof new T\u003e = new T(...A);\r\n```\r\nwhere `new T` takes the constructor type of `T` and turns it into a function type\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\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. library functionality, non-ECMAScript syntax with JavaScript output, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n","author":{"url":"https://github.com/lazytype","@type":"Person","name":"lazytype"},"datePublished":"2020-08-21T11:46:03.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/40179/TypeScript/issues/40179"}
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:0ce128d2-4c4e-13a0-5389-80660dc81954 current-catalog-service-hash 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 request-id ED54:C1BD7:6EFBA0:9D05D4:6A6306DE html-safe-nonce 177b0decfb32a3c60a622f43b3709fee9296613ff93a8c39a898c0dce56e0615 visitor-payload eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDU0OkMxQkQ3OjZFRkJBMDo5RDA1RDQ6NkE2MzA2REUiLCJ2aXNpdG9yX2lkIjoiODg4NDI2NTI0MDgzNjExNDE0MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 visitor-hmac af7bd1b6ed82385db02e040b94f8b30b171241c1bf54a3eb78d2794c34ef21f2 hovercard-subject-tag issue:683503798 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/40179/issue_layout twitter:image https://opengraph.githubassets.com/c094d11bac6f796b6ad07c072ac3577d0d3a8cf01d0a1bdc0765a237064bb32d/microsoft/TypeScript/issues/40179 twitter:card summary_large_image og:image https://opengraph.githubassets.com/c094d11bac6f796b6ad07c072ac3577d0d3a8cf01d0a1bdc0765a237064bb32d/microsoft/TypeScript/issues/40179 og:image:alt Search Terms overload infer function operator arguments parameters return Suggestion // Already possible type ValueForKey og:image:width 1200 og:image:height 600 og:site_name GitHub og:type object og:author:username lazytype hostname github.com expected-hostname github.com None b48e7bf0b1945d51f303503b057abf666ce74c3efbe107b413db2bb921c3f012 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 9370f37042c29822d6ba7e3f670f0036a0155b21 ui-target full theme-color #1e2327 color-scheme light dark
URLs of crawlers that visited me.