Title: Suggestion: Enable inferring the result type of a generic function based on the context in which it is used · Issue #3423 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: Enable inferring the result type of a generic function based on the context in which it is used · Issue #3423 · microsoft/TypeScript
X Title: Suggestion: Enable inferring the result type of a generic function based on the context in which it is used · Issue #3423 · microsoft/TypeScript
Description: It's a very common situation in our code when we wish TypeScript could infer the result type of a function with a generic result based on the context in which that function is used. Here are a couple of use cases to support the feature r...
Open Graph Description: It's a very common situation in our code when we wish TypeScript could infer the result type of a function with a generic result based on the context in which that function is used. Here are a coup...
X Description: It's a very common situation in our code when we wish TypeScript could infer the result type of a function with a generic result based on the context in which that function is used. Here are a ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/3423
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: Enable inferring the result type of a generic function based on the context in which it is used","articleBody":"It's a very common situation in our code when we wish TypeScript could infer the result type of a function with a generic result based on the context in which that function is used. Here are a couple of use cases to support the feature request:\n\nCase 1: Generic fail function (for being able to throw from an expression)\n\n``` typescript\nfunction fail\u003cr>(message: string) : r { throw new Error(message); }\nfunction id\u003ca>(value: a) : a { return value; }\nfunction withFew\u003ca, r>(values: a[], haveFew: (values: a[]) => r, haveNone: (reason: string) => r) : r {\n return values.length > 0 ? haveFew(values) : haveNone('Array is empty.');\n}\nvar values = Math.random() > 0.5 ? ['a', 'b', 'c'] : [];\nvar few = withFew(values, id, fail); // \u003c-- actual result is {}, expected is string[]\n```\n\nCase 2: Empty container constructors:\n\n``` typescript\nfunction toArrayOf\u003ca>() : a[] { return []; }\nfunction id\u003ca>(value: a) : a { return value; }\nfunction withSome\u003ca>(valueOpt: a, haveSome: (value: a) => r, haveNone: (reason: string) => r) : r {\n return valueOpt != null ? haveSome(valueOpt) : haveNone('Value is unspecified.');\n}\nvar valuesOpt = Math.random() > 0.5 ? ['a', 'b', 'c'] : null;\nvar values = withSome(valueOpt, id, toArrayOf); // \u003c-- actual result is {}, expected is string[]\n```\n","author":{"url":"https://github.com/zpdDG4gta8XKpMCd","@type":"Person","name":"zpdDG4gta8XKpMCd"},"datePublished":"2015-06-08T18:42:01Z","url":"https://github.com/microsoft/TypeScript/issues/3423"}
| 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:c567a3d8-e318-23f0-3c7a-648c266f569c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DD8A:19114D:BF4CF2:F734DF:6B00EA6D |
| html-safe-nonce | 87bcf2769ce04c0245bd9f0be4dd00f5db4bb83ebfaf53da514b1b347b66beaa |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERDhBOjE5MTE0RDpCRjRDRjI6RjczNERGOjZCMDBFQTZEIiwidmlzaXRvcl9pZCI6IjkwNDc5MDYwODAzOTQzNzM3NDEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 714e03cc60e98158634b663ee8c757585217f3ae536ba567bf4b76bed1870a49 |
| hovercard-subject-tag | issue:86287002 |
| 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/3423/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b343958a617c96735ef734ffd0886b7345ebcb683918d209aaaaf46066ae4393/microsoft/TypeScript/issues/3423 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b343958a617c96735ef734ffd0886b7345ebcb683918d209aaaaf46066ae4393/microsoft/TypeScript/issues/3423 |
| og:image:alt | It's a very common situation in our code when we wish TypeScript could infer the result type of a function with a generic result based on the context in which that function is used. Here are a coup... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zpdDG4gta8XKpMCd |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width