Title: Contextual parameters inferred from overloads improvements · Issue #59350 · microsoft/TypeScript · GitHub
Open Graph Title: Contextual parameters inferred from overloads improvements · Issue #59350 · microsoft/TypeScript
X Title: Contextual parameters inferred from overloads improvements · Issue #59350 · microsoft/TypeScript
Description: 🔍 Search Terms contextual parameters functions overloads inference TS7006 argument length ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of existi...
Open Graph Description: 🔍 Search Terms contextual parameters functions overloads inference TS7006 argument length ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn...
X Description: 🔍 Search Terms contextual parameters functions overloads inference TS7006 argument length ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wo...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/59350
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Contextual parameters inferred from overloads improvements","articleBody":"### 🔍 Search Terms\n\ncontextual parameters functions overloads inference TS7006 argument length\n\n### ✅ Viability Checklist\n\n- [X] This wouldn't be a breaking change in existing TypeScript/JavaScript code\n- [X] This wouldn't change the runtime behavior of existing JavaScript code\n- [X] This could be implemented without emitting different JS based on the types of the expressions\n- [X] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\n- [X] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types\n- [X] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals\n\n### ⭐ Suggestion\n\nCurrently, inferring a parameter type from a function with overloads is very finicky. Unused generic parameters, constraints, and parameter length all unnecessarily prevent parameter types from being inferred.\r\n\r\nA lot of these limitations are mentioned on https://github.com/microsoft/TypeScript/pull/42620, but there's no inherent reason the parameters can't be inferred in many of these cases. This is a suggestion to revisit some of the merge logic to allow a wider variety of cases to be inferred correctly.\n\n### 📃 Motivating Example\n\nHere are some examples I've run into recently that I believe are reasonable inference candidates:\r\n\r\nPlayground: https://tsplay.dev/N5YkVN\r\n\r\n```ts\r\nexport type Fn = {\r\n // t is unused in the first overload\r\n \u003ct>(s: string): void\r\n \u003ct>(t: t): void\r\n}\r\n\r\n// first: string | t\r\nexport const fn: Fn = (first) => {}\r\n\r\nexport type Fn2 = {\r\n // remove unused generic param\r\n (s: string): void\r\n \u003ct>(t: t): void\r\n}\r\n\r\n// Parameter 'first' implicitly has an 'any' type.\r\nexport const fn2: Fn2 = (first) => {}\r\n\r\nexport type Fn3 = {\r\n \u003ct>(s: string): void\r\n // add constraint to a parameter\r\n \u003ct extends number>(t: t): void\r\n}\r\n\r\n// Parameter 'first' implicitly has an 'any' type.\r\nexport const fn3: Fn3 = (first) => {}\r\n\r\n\r\nexport type Fn4 = {\r\n (s: string): void\r\n (n: number, b: boolean): void\r\n}\r\n\r\n// Second could safely be inferred as boolean | undefined here\r\n\r\n// Type '(first: number, second: boolean) => void' is not assignable to type 'Fn4'.\r\n// Target signature provides too few arguments. Expected 2 or more, but got 1.(2322)\r\nexport const fn4: Fn4 = (first, second) => {}\r\n```\n\n### 💻 Use Cases\n\n1. What do you want to use this for? Parameter inference\r\n2. What shortcomings exist with current approaches? Duplicate definitions and casting\r\n3. What workarounds are you using in the meantime? Forcing the signatures to align with hacks like adding unused generic parameters, manually typing them and/or casting.\r\n","author":{"url":"https://github.com/ssalbdivad","@type":"Person","name":"ssalbdivad"},"datePublished":"2024-07-18T17:48:27Z","url":"https://github.com/microsoft/TypeScript/issues/59350"}
| 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:0ae14fae-b608-cbde-19cc-9f7a37aef7b7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AC6E:27D3AD:755402:96C318:6B00E405 |
| html-safe-nonce | 4ab41b79b1e00b9703dae17a2290180275acccd54af9c859b6151b0371dc8aa9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQzZFOjI3RDNBRDo3NTU0MDI6OTZDMzE4OjZCMDBFNDA1IiwidmlzaXRvcl9pZCI6IjYyMjY3Mzk4ODEyNTU2OTEyNjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2706a709793ccad9ac083c8aac58ec98d785577fde6489e26a9ec27e31a396f9 |
| hovercard-subject-tag | issue:2417012805 |
| 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/59350/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c0865ebba001b761e77cc214de21e8f8a8978785e24c60aa1f5262b1f97f8216/microsoft/TypeScript/issues/59350 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c0865ebba001b761e77cc214de21e8f8a8978785e24c60aa1f5262b1f97f8216/microsoft/TypeScript/issues/59350 |
| og:image:alt | 🔍 Search Terms contextual parameters functions overloads inference TS7006 argument length ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ssalbdivad |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| 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 | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width