Title: Typescript does not implicitly infers types when providing function as arguments? · Issue #17520 · microsoft/TypeScript · GitHub
Open Graph Title: Typescript does not implicitly infers types when providing function as arguments? · Issue #17520 · microsoft/TypeScript
X Title: Typescript does not implicitly infers types when providing function as arguments? · Issue #17520 · microsoft/TypeScript
Description: When providing identity function to map, it's return type is incorrect(see x2), while providing anonymous function as a callback leads to a correct output type(see x1). TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx) Code const ...
Open Graph Description: When providing identity function to map, it's return type is incorrect(see x2), while providing anonymous function as a callback leads to a correct output type(see x1). TypeScript Version: 2.4.0 / ...
X Description: When providing identity function to map, it's return type is incorrect(see x2), while providing anonymous function as a callback leads to a correct output type(see x1). TypeScript Version: 2.4....
Opengraph URL: https://github.com/microsoft/TypeScript/issues/17520
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Typescript does not implicitly infers types when providing function as arguments?","articleBody":"\u003c!-- BUGS: Please use this template. -->\r\nWhen providing `identity` function to `map`, it's return type is incorrect(see `x2`), while providing anonymous function as a callback leads to a correct output type(see `x1`).\r\n\r\n**TypeScript Version:** 2.4.0 / nightly (2.5.0-dev.201xxxxx)\r\n\r\n**Code**\r\n```ts\r\nconst map = \u003cT, U>(f: (x: T) => U, arr: T[]): U[] => {\r\n return arr.map((val) => f(val));\r\n}\r\n\r\n// x1 type is number[]\r\nconst x1 = map(x => x, [1, 2, 3]);\r\n\r\nconst identity = \u003cT>(x: T) => x\r\n// x2 type is {}[]\r\nconst x2 = map(identity, [1, 2, 3]);\r\n```\r\n\r\n**Expected behavior:**\r\nReturn type of `map` function, when providing `identity` function should be `number[]`\r\n\r\n**Actual behavior:**\r\nReturn type is `{}[]`\r\n___\r\nAlso, I've realized that `map` function in 2nd example does not infers even `T` type. It's `{}` currently, while it's obvious should be a `number` type, as we are providing array of numbers:\r\n\u003cimg width=\"652\" alt=\"screen shot 2017-07-30 at 11 47 52 pm\" src=\"https://user-images.githubusercontent.com/12725024/28756993-ab7eefb4-7582-11e7-9bba-35b1674c641e.png\">\r\n\r\nThe second interesting thing is, that type inferring works fine, if `map` function arguments will be reversed:\r\n```ts\r\nconst map = \u003cT, U>(arr: T[], f: (x: T) => U): U[] => {\r\n return arr.map((val) => f(val));\r\n}\r\n// x2 type is number[]\r\nconst x2 = map1([1, 2, 3], identity);\r\n```","author":{"url":"https://github.com/1ven","@type":"Person","name":"1ven"},"datePublished":"2017-07-30T21:05:37Z","url":"https://github.com/microsoft/TypeScript/issues/17520"}
| 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:ec5a370b-baab-31de-56e2-3f5d944b8a18 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D6E8:363D8E:99B8E1:C6D1CA:6B00DE5F |
| html-safe-nonce | 7e349566b4f952ff4b186462f439b3766cdee303e59ca757296011db50068c0c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENkU4OjM2M0Q4RTo5OUI4RTE6QzZEMUNBOjZCMDBERTVGIiwidmlzaXRvcl9pZCI6IjMwMTkyNzQzNzIwNzU1NDQxNTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d28cac7a0b9f56cf66f3ad026008ff2589309cc6561332a4449d8a3a0e0816a5 |
| hovercard-subject-tag | issue:246608901 |
| 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/17520/issue_layout |
| twitter:image | https://opengraph.githubassets.com/565301280e357dcf40dbe38a9d34c8f8751b415a2524f3cd472256fcd27495cc/microsoft/TypeScript/issues/17520 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/565301280e357dcf40dbe38a9d34c8f8751b415a2524f3cd472256fcd27495cc/microsoft/TypeScript/issues/17520 |
| og:image:alt | When providing identity function to map, it's return type is incorrect(see x2), while providing anonymous function as a callback leads to a correct output type(see x1). TypeScript Version: 2.4.0 / ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | 1ven |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| 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 | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width