Title: Inferring a generic should put the type variable in the inner scope when possible · Issue #49505 · microsoft/TypeScript · GitHub
Open Graph Title: Inferring a generic should put the type variable in the inner scope when possible · Issue #49505 · microsoft/TypeScript
X Title: Inferring a generic should put the type variable in the inner scope when possible · Issue #49505 · microsoft/TypeScript
Description: Suggestion 🔍 Search Terms generics polymorphism type inference ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavio...
Open Graph Description: Suggestion 🔍 Search Terms generics polymorphism type inference ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code ...
X Description: Suggestion 🔍 Search Terms generics polymorphism type inference ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript c...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/49505
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inferring a generic should put the type variable in the inner scope when possible","articleBody":"# Suggestion\r\n\r\n## 🔍 Search Terms\r\n\r\n* generics\r\n* polymorphism\r\n* type inference\r\n\r\n## ✅ Viability 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, new syntax sugar for JS, 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\r\n## ⭐ Suggestion\r\n\r\nWhen inferring a generic type, put the type variable in the inner scope as long as it's possible to make the result as general as possible.\r\n\r\n## 📃 Motivating Example\r\n\r\nSay I have a simple composition function:\r\n\r\n```typescript\r\ndeclare const compose: \u003cA, B, C\u003e(f: (a: A) =\u003e B, g: (b: B) =\u003e C) =\u003e (a: A) =\u003e C\r\n```\r\n\r\nThe current version of TS allows me to do this and get the correct type:\r\n\r\n```typescript\r\nconst comp = compose(\u003cT\u003e(t: T) =\u003e t, \u003cT\u003e(t: T) =\u003e t); // comp: \u003cT\u003e(a: T) =\u003e T\r\n```\r\n\r\nBut say I want to wrap the result in an object:\r\n\r\n```typescript\r\ndeclare const composeObj: \u003cA, B, C\u003e(f: (a: A) =\u003e B, g: (b: B) =\u003e C) =\u003e { value: (a: A) =\u003e C }\r\n```\r\n\r\nNow the generic is gone, and `comp` above is typed as `{ value: (a: unknown) =\u003e unknown }`. \r\n\r\nI think what's happening is that TS is trying to put the `T` variable outside the scope of the object because if, instead of a wrapping object, I use a wrapping function, this is what happens:\r\n\r\n```typescript\r\ndeclare const composeFun: \u003cA, B, C\u003e(f: (a: A) =\u003e B, g: (b: B) =\u003e C) =\u003e () =\u003e (a: A) =\u003e C\r\nconst comp = composeFun(\u003cT\u003e(t: T) =\u003e t, \u003cT\u003e(t: T) =\u003e t); // comp: \u003cT\u003e() =\u003e (a: T) =\u003e T\r\n```\r\n\r\nTS is putting `T` on the outer level, so if I call `comp()` the result will be `(a: unknown) =\u003e unknown`. A better result would've been to get `() =\u003e \u003cT\u003e(a: T) =\u003e T`. This way, calling `comp()` would not discard the type variable and, if I'm not mistaken, the result would be a more general type than the result we currently get, without breaking any typing rules.\r\n\r\n## 💻 Use Cases\r\n\r\nBeing able to wrap generic functions when they're the result of type inference. My specific case was writing a type guarding/converting utility using a wrapper around functions of the form `a -\u003e Maybe\u003cb\u003e` that can be chained together by the dot operator to represent function composition, such that types can be further specified. Something like `Thing.isPrimitive.isNumber.isInteger`. But this is not possible without the possibility to wrap the result of my `compose` function. \r\n","author":{"url":"https://github.com/jsoldi","@type":"Person","name":"jsoldi"},"datePublished":"2022-06-12T18:30:53.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/49505/TypeScript/issues/49505"}
| 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:d1e0b3d8-06e3-f51c-cb54-8db53c63fab8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A778:17973D:18AA201:226790D:6A61CED9 |
| html-safe-nonce | aada6728bd51f3a1771d0e511db76c08040cf1212214077e1c0f7ecc0cee74f9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzc4OjE3OTczRDoxOEFBMjAxOjIyNjc5MEQ6NkE2MUNFRDkiLCJ2aXNpdG9yX2lkIjoiNDgyMTI2NjE1MjA4MDY1ODEzNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ed1a6896f729837afb9aeebcc4c67950143f82cfc9c8911582177ef265404f20 |
| hovercard-subject-tag | issue:1268664916 |
| 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/49505/issue_layout |
| twitter:image | https://opengraph.githubassets.com/70bc67e1ad6dadf9470c14425cec6a5a565ff1648034e5acbf87e420ee62992b/microsoft/TypeScript/issues/49505 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/70bc67e1ad6dadf9470c14425cec6a5a565ff1648034e5acbf87e420ee62992b/microsoft/TypeScript/issues/49505 |
| og:image:alt | Suggestion 🔍 Search Terms generics polymorphism type inference ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jsoldi |
| hostname | github.com |
| expected-hostname | github.com |
| None | b2de8c74e5e61e893155ba46ee41bc66170c1644cb795adefa8386d490f7781c |
| 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 | d1866027ded575df8a15c731dd8b9986c9483ceb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width