Title: Proposal: better type narrowing in overloaded functions through overload set pruning · Issue #22609 · microsoft/TypeScript · GitHub
Open Graph Title: Proposal: better type narrowing in overloaded functions through overload set pruning · Issue #22609 · microsoft/TypeScript
X Title: Proposal: better type narrowing in overloaded functions through overload set pruning · Issue #22609 · microsoft/TypeScript
Description: TypeScript Version: 2.7.2 Search Terms: type inference, type narrowing, overloads Code function bar(selector: "A", val: number): number; function bar(selector: "B", val: string): string; function bar(selector: "A" | "B", val: number | st...
Open Graph Description: TypeScript Version: 2.7.2 Search Terms: type inference, type narrowing, overloads Code function bar(selector: "A", val: number): number; function bar(selector: "B", val: string): string; function b...
X Description: TypeScript Version: 2.7.2 Search Terms: type inference, type narrowing, overloads Code function bar(selector: "A", val: number): number; function bar(selector: "B", val: string)...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/22609
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: better type narrowing in overloaded functions through overload set pruning","articleBody":"**TypeScript Version:** 2.7.2\r\n\r\n**Search Terms:** type inference, type narrowing, overloads\r\n\r\n**Code**\r\n\r\n```ts\r\nfunction bar(selector: \"A\", val: number): number;\r\nfunction bar(selector: \"B\", val: string): string;\r\nfunction bar(selector: \"A\" | \"B\", val: number | string): number | string\r\n{\r\n if (selector === \"A\")\r\n {\r\n const the_selector = selector; // \"A\"\r\n const the_val = val; // number | string but should be just number\r\n return \"foo\"; // should give error: when selector is \"A\" should return a number\r\n }\r\n else\r\n {\r\n const the_selector = selector; // \"B\"\r\n const the_val = val; // number | string but should be just string\r\n return 42; // should give error: when selector is \"B\" should return a string\r\n }\r\n}\r\n```\r\n\r\n**Issue**\r\n\r\nThe snippet above shows a limitation of the current type inference implementation in TypeScript.\r\nAs can be clearly seen from the overloaded declarations, when `selector === \"A\"` we have `val: number` and we must return a `number`, while when `selector === \"B\"` we have `val: string` and we must return a `string`. But the type-checker, although able to understand that `the_selector` can only be `\"A\"` in the `if` block and only `\"B\"` in the `else` block, still types `the_val` as `number | string` in both blocks and allows us to return a `string` when `selector === \"A\"` and a `number` when `selector === \"B\"`.\r\n\r\n**Possible Solution**\r\n\r\nIn the implementation of an overloaded function, when the type-checker decides to narrow the type of a parameter (as often happens in `if-else` blocks), it should also remove the overloads that don't match the narrowed parameter type from the overload set, and use the pruned overload set to narrow the types of the other parameters.\r\n\r\n**Playground Link:** https://www.typescriptlang.org/play/#src=%0Afunction%20bar(selector%3A%20%22A%22%2C%20val%3A%20number)%3A%20number%3B%0Afunction%20bar(selector%3A%20%22B%22%2C%20val%3A%20string)%3A%20string%3B%0Afunction%20bar(selector%3A%20%22A%22%20%7C%20%22B%22%2C%20val%3A%20number%20%7C%20string)%3A%20number%20%7C%20string%0A%7B%0A%20%20%20%20if%20(selector%20%3D%3D%3D%20%22A%22)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20const%20the_selector%20%3D%20selector%3B%0A%20%20%20%20%20%20%20%20const%20the_val%20%3D%20val%3B%0A%20%20%20%20%20%20%20%20return%20%22foo%22%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20const%20the_selector%20%3D%20selector%3B%0A%20%20%20%20%20%20%20%20const%20the_val%20%3D%20val%3B%0A%20%20%20%20%20%20%20%20return%2042%3B%0A%20%20%20%20%7D%0A%7D\r\n","author":{"url":"https://github.com/lodo1995","@type":"Person","name":"lodo1995"},"datePublished":"2018-03-15T18:18:37Z","url":"https://github.com/microsoft/TypeScript/issues/22609"}
| 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:b0e5c03a-a356-d6b0-462c-b80bdfc03570 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF14:1CE302:AAC8B3:E94F11:6B0451A1 |
| html-safe-nonce | 3b51bb8532afd47dc1ec43c56c85919a885af4c8249a89fbf72512fa61172b11 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjE0OjFDRTMwMjpBQUM4QjM6RTk0RjExOjZCMDQ1MUExIiwidmlzaXRvcl9pZCI6Ijk4ODk3MjMzNTQ0NzE2NzM5MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fc18d52b92a4cfdd0409e32f374b8d766263284428308f119e1a11560a1e5069 |
| hovercard-subject-tag | issue:305666718 |
| 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/22609/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bf0c9ac33f9cdfe7bf7d9e29c0c924635f5b5bcea7c6472f74059cf9af599102/microsoft/TypeScript/issues/22609 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bf0c9ac33f9cdfe7bf7d9e29c0c924635f5b5bcea7c6472f74059cf9af599102/microsoft/TypeScript/issues/22609 |
| og:image:alt | TypeScript Version: 2.7.2 Search Terms: type inference, type narrowing, overloads Code function bar(selector: "A", val: number): number; function bar(selector: "B", val: string): string; function b... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lodo1995 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 910ca0e260cfec6aff11407d8dc6a0d3a4cd381a2ec9ada56627b063426892a0 |
| 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 | e086f9b7e88322eb2b41885a6ad425b215737fa5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width