Title: Can't call `((_: string) => T) | undefined` when conditionally `string extends T` · Issue #43425 · microsoft/TypeScript · GitHub
Open Graph Title: Can't call `((_: string) => T) | undefined` when conditionally `string extends T` · Issue #43425 · microsoft/TypeScript
X Title: Can't call `((_: string) => T) | undefined` when conditionally `string extends T` · Issue #43425 · microsoft/TypeScript
Description: Bug Report I am trying to make a generic callback function (_: string) => T, that is allowed to be optional (undefined) only when T is string. (Specifically, I chose string extends T because TS doesn't seem to provide exact equality for ...
Open Graph Description: Bug Report I am trying to make a generic callback function (_: string) => T, that is allowed to be optional (undefined) only when T is string. (Specifically, I chose string extends T because TS doe...
X Description: Bug Report I am trying to make a generic callback function (_: string) => T, that is allowed to be optional (undefined) only when T is string. (Specifically, I chose string extends T because TS ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/43425
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Can't call `((_: string) => T) | undefined` when conditionally `string extends T`","articleBody":"# Bug Report\r\n\r\nI am trying to make a generic callback function `(_: string) => T`, that is allowed to be optional (undefined) only when `T` is `string`. (Specifically, I chose `string extends T` because TS doesn't seem to provide exact equality for conditional types, and I want the default value to be `(x: string) => x`.)\r\n\r\nThe initial way I wrote this was `string extends T ? ((value: string) => T) | undefined : (value: string) => T`, but I found I was unable to (conditionally) call the function. The error seems to stem from TS treating the type as `(value: T & string) => T`, which prevent me from passing in a string literal.\r\n\r\n\u003cimg width=\"590\" alt=\"image\" src=\"https://user-images.githubusercontent.com/14237/112906752-66d32e00-9088-11eb-8109-75a996f84630.png\">\r\n\r\nI found a workaround that allows me to call the function: `((value: string) => T) | (string extends T ? undefined : never)` (**which I believe should be exactly equivalent**, though let me know if this is wrong). However, this too is broken in the latest nightly, seemingly because of how `NonNullable` is being distributed over `|`:\r\n\r\n\u003cimg width=\"607\" alt=\"Screen Shot 2021-03-29 at 12 17 07 PM\" src=\"https://user-images.githubusercontent.com/14237/112906960-c4677a80-9088-11eb-913f-f007ca22b972.png\">\r\n\r\n\r\n### 🔎 Search Terms\r\n\r\ngeneric extends function parameter union 🤷\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This changed between versions 3.8.3 and 3.9.7, and changed again between 4.2.2 and the nightly.\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?#code/C4TwDgpgBAKgTgQwHYGcBmB7OBbAjAHhgD4oBeKACgoDcEAbAVwgC4oVg4BLJAcwEoyJGAIA+ldl15QIAD2AQkAExSwoAfigMlENNwiKorJBGoQ4fANwBYAFChIsRKkw4ATIRLkJ3HtLkLlVQ0qWkYWNg4fAVIhUU1tXWMDVhp6JlZvXmihaxtbNC0AY2BODCQoeXYPCjRcVnhkdCw8DwAaKDRXeqcmtw8BAG9bKA7cNQA6CgAiAAsIOjoMKcthjtcJ6bmFpZWbAF9bIA)\r\n\r\n### 💻 Code\r\n\r\n\u003c!-- Please post the relevant code sample here as well-->\r\n```ts\r\ntype Transform1\u003cT> = ((value: string) => T) | (string extends T ? undefined : never);\r\ntype Transform2\u003cT> = string extends T ? ((value: string) => T) | undefined : (value: string) => T;\r\n\r\nfunction test\u003cT>(f1: Transform1\u003cT>, f2: Transform2\u003cT>) {\r\n f1?.(\"hello\"); // error in nightly, no error in 4.2.2\r\n f2?.(\"hello\"); // error since 3.9.7, no error in 3.8.3\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nError on the call to `f1` in 4.2.2, and errors on both calls in the nightly.\r\n\r\n### 🙂 Expected behavior\r\n\r\nBoth calls should succeed. The function return value is ignored, so `string extends T` should have no impact on the validity of the expression.\r\n","author":{"url":"https://github.com/jtbandes","@type":"Person","name":"jtbandes"},"datePublished":"2021-03-29T22:21:19Z","url":"https://github.com/microsoft/TypeScript/issues/43425"}
| 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:59f08402-cf52-48d9-097f-181f4ef16593 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D5D0:B1B84:19496A0:201A2FA:6B01839E |
| html-safe-nonce | a0bce14819f0448feeefb5569f1a19104f5758dd4bc5a8eaf4ac4e0ddcad89fa |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENUQwOkIxQjg0OjE5NDk2QTA6MjAxQTJGQTo2QjAxODM5RSIsInZpc2l0b3JfaWQiOiI1NDUwNDU1MTgyODAwNDIxNzkwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 143d05e49d56f10481de6d3d1b8a23620ada37736b5b2b481a7c8bdb6d082c7b |
| hovercard-subject-tag | issue:843863328 |
| 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/43425/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e3e25e1674d462df7818d78383d10f22566ad5a6667b56b72a03a930e03a4662/microsoft/TypeScript/issues/43425 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e3e25e1674d462df7818d78383d10f22566ad5a6667b56b72a03a930e03a4662/microsoft/TypeScript/issues/43425 |
| og:image:alt | Bug Report I am trying to make a generic callback function (_: string) => T, that is allowed to be optional (undefined) only when T is string. (Specifically, I chose string extends T because TS doe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jtbandes |
| 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