Title: ReturnType not being evaluated when used in function definition generics alias · Issue #41391 · microsoft/TypeScript · GitHub
Open Graph Title: ReturnType not being evaluated when used in function definition generics alias · Issue #41391 · microsoft/TypeScript
X Title: ReturnType not being evaluated when used in function definition generics alias · Issue #41391 · microsoft/TypeScript
Description: Scenario: Given an indexed object of functions that return different value types, generate a new object with the same indexes and the return value of the functions. const given = { foo: () => 1, bar: () => 'milka', }; const generatedResu...
Open Graph Description: Scenario: Given an indexed object of functions that return different value types, generate a new object with the same indexes and the return value of the functions. const given = { foo: () => 1, ba...
X Description: Scenario: Given an indexed object of functions that return different value types, generate a new object with the same indexes and the return value of the functions. const given = { foo: () => 1,...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/41391
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ReturnType not being evaluated when used in function definition generics alias","articleBody":"### Scenario:\r\nGiven an indexed object of functions that return different value types, generate a new object with the same indexes and the return value of the functions.\r\n```ts\r\nconst given = {\r\n foo: () => 1,\r\n bar: () => 'milka',\r\n};\r\n\r\nconst generatedResult = {\r\n foo: 1,\r\n bar: 'milka',\r\n};\r\n```\r\n\r\n### Failing example:\r\nTypeScript is not re-evaluating the Types when calling the function with predictable values:\r\n\r\nIt can predict the functions and their return types, but it is not evaluating `ReturnType\u003c...>`\r\n\r\n### Workaround example:\r\nThe proper types are correctly inferred when using an anonymous type directly in the return type of the function:\r\n\r\n\r\nThis workaround is not desired given the need for using the Type definition inside the body of the function.\r\n\r\n**TypeScript Version:** 4.0.3\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\n- ReturnType\r\n- Generics\r\n\r\n**Code**\r\n\r\n```ts\r\nfunction extract\u003c\r\n P extends string,\r\n M extends Record\u003cP, () => any>,\r\n Ret extends {\r\n [K in P]: ReturnType\u003cM[K]>;\r\n }\r\n>(_map: M): Ret {\r\n return \u003cRet>{};\r\n}\r\n\r\nconst { foo, bar } = extract({\r\n foo: () => 1,\r\n bar: () => 'bar',\r\n});\r\n```\r\n\r\n**Expected behavior:**\r\nVariable `foo` should be of type `number`\r\nVariable `bar` should be of type `string`\r\n\r\n**Actual behavior:**\r\nAll variables are of type `any`\r\n\r\n**Playground Link:** \u003c!-- A link to a TypeScript Playground \"Share\" link which demonstrates this behavior -->\r\n[Playground Link](https://www.typescriptlang.org/play?skipLibCheck=true&target=6&jsx=0&ts=4.2.0-dev.20201103#code/GYVwdgxgLglg9mABAUwB5QE4ENoB4BQiiACiusmACYDOi1mMYA5gDSGICyZUFNiASsghwMlXMRaIAFAEpEAXgB8iLGACeitkUFRuvWgG92RANoBpRIxIBdAFwDkUEBjAAVNQAdkuDueuKAbnYAX3xFKQB9AFssD3sOGXsdRCMiDEdnJANglVodIND8YTB6FMRgODhJACMsDEQc+W5saClU8sr7WQVlAEYtRFqMLrklRAByIfG2YJkg-HxQSFgEZpwoACYCIlI0HipaegxGVnYuPf0HYVFxSW6x1Q0wyJi4zkSy80skYjsHJxc7i8Pj8yhyRnSAKyOSwtEeBQWRQQpQMHTgGxqdQ2DQUa2gGza7Aq6JGPUQ-XYQw2pLGkzq03wsyCQA) or a [CodeSandbox Link](https://codesandbox.io/s/returntype-possible-bug-rhyfl) - Both with the same behavior.\r\n\r\n\r\n","author":{"url":"https://github.com/carlos-algms","@type":"Person","name":"carlos-algms"},"datePublished":"2020-11-03T23:36:51Z","url":"https://github.com/microsoft/TypeScript/issues/41391"}
| 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:46d3fbd6-74a2-0a5b-c341-c3f029d5f76e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B1AC:34CAB:2F56952:3C8EE59:6B01D02C |
| html-safe-nonce | a11363ddf5a521336c1866c3c367ac23ff28c04ecf6e00d5ea217736c485b212 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMUFDOjM0Q0FCOjJGNTY5NTI6M0M4RUU1OTo2QjAxRDAyQyIsInZpc2l0b3JfaWQiOiI4MTg1MjA0NTAxMDQ5ODg4ODEyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | a8f37f8fc5c531384a257d7dadc9b052d535bf0b626ae182505dbaa85d02b803 |
| hovercard-subject-tag | issue:735705415 |
| 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/41391/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e9d3dea19b10eed9dab1a293235d7ed6ddb1617c7687d21e4f34bae64d153dc4/microsoft/TypeScript/issues/41391 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e9d3dea19b10eed9dab1a293235d7ed6ddb1617c7687d21e4f34bae64d153dc4/microsoft/TypeScript/issues/41391 |
| og:image:alt | Scenario: Given an indexed object of functions that return different value types, generate a new object with the same indexes and the return value of the functions. const given = { foo: () => 1, ba... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | carlos-algms |
| 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