Title: Function parameters and return type inference with infer keyword · Issue #38182 · microsoft/TypeScript · GitHub
Open Graph Title: Function parameters and return type inference with infer keyword · Issue #38182 · microsoft/TypeScript
X Title: Function parameters and return type inference with infer keyword · Issue #38182 · microsoft/TypeScript
Description: I saw a couple of similar issues, but it all provided very poor specification of the problem, so I decided to open my own issue. Description The further step in bringing nice fp declarations into TypeScript is adding function parameters ...
Open Graph Description: I saw a couple of similar issues, but it all provided very poor specification of the problem, so I decided to open my own issue. Description The further step in bringing nice fp declarations into T...
X Description: I saw a couple of similar issues, but it all provided very poor specification of the problem, so I decided to open my own issue. Description The further step in bringing nice fp declarations into T...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/38182
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Function parameters and return type inference with infer keyword","articleBody":"I saw a couple of similar issues, but it all provided very poor specification of the problem, so I decided to open my own issue.\r\n\r\n# Description\r\nThe further step in bringing nice fp declarations into TypeScript is adding function parameters and return type inference through `infer` keyword.\r\n\r\nLets look at some examples:\r\n\r\n```typescript\r\ntype F1 = \u003cT extends number>(a: T, b: T) => T\r\n\r\n// Now: [number, number]\r\n// Propose: [1, 1]\r\ntype R1 = F1 extends ((a: 1, b: infer P) => infer R) ? [P, R] : never\r\n```\r\n\r\nHere we passed parameter `a` as `1` and expect:\r\n1. Type parameter `T` to be inferred as `1`\r\n2. Type `P` to be inferred as `T` -> `1`\r\n3. Type `R` to be inferred as `T` -> `1` \r\n4. Result to be inferred as `[P, R]` -> `[1, 1]` \r\n\r\n---\r\n\r\n```typescript\r\ntype F2 = \u003cT1 extends AnyTuple, T2>(a: T1, b: T2, c: Reverse\u003cT1>) => [T1, T2] \r\n\r\n// Now: [never, unknown, [AnyTuple, unknown]]\r\n// Propose: [[3, 2, 1], unknown, [[1, 2, 3], unknown]]\r\ntype R2 = F2 extends (a: [1, 2, 3], b: infer P2, c: infer P1) => infer R ? [P1, P2, R] : never\r\n```\r\n\r\nHere we passed parameter `a` as `[1, 2, 3]` and expect:\r\n1. Type parameter `T1` to be inferred as `[1, 2, 3]`\r\n2. Type parameter `T2` to be replaced with `unknown` type because we provided no info to be able to infer it \r\n3. Type `P2` to be inferred as `T2` -> `unknown` \r\n4. Type `P1` to be inferred as `Reverse\u003cT1>` -> `Reverse\u003c[1, 2, 3]>` -> `[3, 2, 1]`\r\n5. Type `R` to be inferred as `[T1, T2]` -> `[[1, 2, 3], unknown]`\r\n6. Result to be inferred as `[P1, P2, R]` -> `[[3, 2, 1], unknown, [[1, 2, 3], unknown]]\r\n\r\n---\r\n\r\n```typescript\r\ntype F3 = \u003cT extends AnyTuple>(a: T, b: T['length']) => typeof b\r\n\r\n// Now: never\r\n// Propose: 3\r\ntype R3 = F3 extends ((a: [1, 2, 3], b: infer LEN) => any) ? LEN : never\r\n```\r\n\r\nHere we expect:\r\n1. Type parameter `T` to be inferred as `[1, 2, 3]`\r\n2. Type LEN to be inferred as `T['length']` -> `[1, 2, 3]['length']` -> `3`\r\n3. Result to be inferred as `LEN` -> `3`\r\n\r\n---\r\n\r\nCase of [wrapping type checkers](https://dev.to/awerlogus/why-you-check-the-types-wrong-wrapping-type-checkers-ke):\r\n\r\n```typescript\r\nenum string_literal { string_literal = '' }\r\n\r\ntype StringLiteral\u003cT> = T extends string ? string extends T ? string_literal : T : string_literal\r\n\r\ntype F4 = \u003cT>(a: StringLiteral\u003cT>, b: T) => T extends '' ? [] : T\r\n\r\n// Now: all cases never\r\n\r\n// Proposed: ['foobar', 'foobar']\r\ntype R4_1 = F4 extends ((a: 'foobar', b: infer P) => infer R) ? [P, R] : never\r\n// Proposed: ['', []]\r\ntype R4_2 = F4 extends ((a: '', b: infer P) => infer R) ? [P, R] : never\r\n// Proposed: [string_literal, string]\r\ntype R4_3 = F4 extends ((a: string, b: infer P) => infer R) ? [P, R] : never\r\n```\r\n\r\n---\r\n\r\n# Use cases\r\n\r\nIn other similar issues I saw only use cases of function parameters inference, so I'll extend it with return type inference cases.\r\n\r\nThe first example is a `call` function:\r\n\r\n```typescript\r\ndeclare function call\u003cP extends AnyTuple, F extends ((...args: P) => unknown)>(\r\n params: P, func: F\r\n): F extends (...args: P) => infer R ? R : never\r\n\r\n// Got: [unknown, unknown]\r\n// Expected: [1, 2]\r\nconst a = call([1, 2], \u003cA, B>(a: A, b: B): [A, B] => [a, b])\r\n```\r\n\r\nAlso it may be used to infer return types on each step of computing `pipe` and `flow` function versions with unlimited parameters count. I'm sure, there're much more cases where this feature may be applied, but I can't remember any more right now.\r\n\r\n# Util types used\r\n\r\n```typescript\r\ntype Empty = readonly []\r\n\r\n\r\ntype AnyTuple = ReadonlyArray\u003cunknown> & { readonly 0: unknown } | Empty\r\n\r\n\r\ntype Tail\u003cT extends AnyTuple> = ((...args: T) => any) extends (head: any, ...tail: infer R) => any\r\n\r\n ? R extends AnyTuple ? Readonly\u003cR> : never : Empty\r\n\r\n\r\ntype Prepend\u003cT extends AnyTuple, E> = ((e: E, ...args: T) => any) extends\r\n\r\n (...args: infer R) => any ? R extends AnyTuple ? Readonly\u003cR> : never : never\r\n\r\n\r\ntype _ReverseWith\u003cT extends AnyTuple, Result extends AnyTuple> = {\r\n 0: _ReverseWith\u003cTail\u003cT>, Prepend\u003cResult, T[0]>>\r\n 1: Readonly\u003cResult>\r\n}[T extends Empty ? 1 : 0]\r\n\r\n\r\ntype Reverse\u003cT extends AnyTuple> = _ReverseWith\u003cT, Empty>\r\n```\r\n","author":{"url":"https://github.com/awerlogus","@type":"Person","name":"awerlogus"},"datePublished":"2020-04-25T15:16:10Z","url":"https://github.com/microsoft/TypeScript/issues/38182"}
| 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:9f783d2c-fe65-c08b-312f-028a4cdab993 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A960:311640:215CC6E:2A64074:6B0177FF |
| html-safe-nonce | fff8f6e83ef1a80b99c13acbb33f1c1fe34e6b6263607581143d1b02a6e0ca78 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOTYwOjMxMTY0MDoyMTVDQzZFOjJBNjQwNzQ6NkIwMTc3RkYiLCJ2aXNpdG9yX2lkIjoiMzY2NzE5MjQyMzcyMjg3NDg3OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c99bb3941d236744a51e288a0742ab57eb9b2c9ffe9bc548033532601cd10b18 |
| hovercard-subject-tag | issue:606790691 |
| 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/38182/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fc00390be95fb6ab4516ec6767f79b3d4428a63b81e169f37d0e342a553789f4/microsoft/TypeScript/issues/38182 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fc00390be95fb6ab4516ec6767f79b3d4428a63b81e169f37d0e342a553789f4/microsoft/TypeScript/issues/38182 |
| og:image:alt | I saw a couple of similar issues, but it all provided very poor specification of the problem, so I decided to open my own issue. Description The further step in bringing nice fp declarations into T... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | awerlogus |
| 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