Title: Possibly empty tuple confuses map and reduce methods · Issue #38514 · microsoft/TypeScript · GitHub
Open Graph Title: Possibly empty tuple confuses map and reduce methods · Issue #38514 · microsoft/TypeScript
X Title: Possibly empty tuple confuses map and reduce methods · Issue #38514 · microsoft/TypeScript
Description: (feel free to improve the issue title) TypeScript Version: 4.0.0-dev.20200512 (also tested on 3.8.3 and 3.9.2) Search Terms: empty tuple union never map reduce filter forEach Code function f(a: [number, number] | [number] | []) { a.map(x...
Open Graph Description: (feel free to improve the issue title) TypeScript Version: 4.0.0-dev.20200512 (also tested on 3.8.3 and 3.9.2) Search Terms: empty tuple union never map reduce filter forEach Code function f(a: [nu...
X Description: (feel free to improve the issue title) TypeScript Version: 4.0.0-dev.20200512 (also tested on 3.8.3 and 3.9.2) Search Terms: empty tuple union never map reduce filter forEach Code function f(a: [nu...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/38514
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Possibly empty tuple confuses map and reduce methods","articleBody":"*(feel free to improve the issue title)*\r\n\r\n**TypeScript Version:** 4.0.0-dev.20200512 (also tested on 3.8.3 and 3.9.2)\r\n\r\n**Search Terms:** empty tuple union never map reduce filter forEach\r\n\r\n**Code**\r\n```ts\r\nfunction f(a: [number, number] | [number] | []) {\r\n a.map(x => x+3)\r\n a.reduce((acc, next) => acc+next, 0)\r\n a.filter(x => x > 5)\r\n a.forEach(x => x.toString())\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nNo type checking errors: when `a` is an empty tuple at runtime, none of the callbacks would ever be invoked, so their arguments could safely be inferred to be `number` (I guess that internally, `number` and `never` would have to combine to become `number`).\r\n\r\n**Actual behavior:**\r\n\r\nThe `a.map` on line 2 infers the type of `x` to be `any`, and raises an error about incompatible signatures:\r\n\r\n> This expression is not callable.\r\n> Each member of the union type '(\u003cU>(callbackfn: (value: number, index: number, array: number[]) => U, thisArg?: any) => U[]) | (\u003cU>(callbackfn: (value: number, index: number, array: number[]) => U, thisArg?: any) => U[]) | (\u003cU>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])' has signatures, but none of those signatures are compatible with each other.(2349)\r\n\r\nLikewise, the `a.reduce` on line 3 infers the types of `acc` and `next` to be `any`, and raises an error about incompatible signatures:\r\n\r\n> This expression is not callable.\r\n> Each member of the union type '{ (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; \u003cU>(callbackfn: (previousValue: U, currentValue: number, ...' has signatures, but none of those signatures are compatible with each other.(2349)\r\n\r\nFor further information: my original problem in a project (with the same code and same typescript versions) was that each of the methods above (including `a.filter` and `a.foreach` on line 4 and 5) infers its callback’s argument type(s) to be `never`, thus raising further errors (e.g. `Property 'toString' does not exist on type 'never'.ts(2339)`). I am not sure why I cannot reproduce that behaviour in a clean installation or in the playground, or vice versa.\r\n\r\n**[Playground Link](https://www.typescriptlang.org/play/index.html#code/GYVwdgxgLglg9mABMAFAQwFyINphAWwCMBTAJwBpE8iyBdRAHx2pNPqe1oEpEBvAKERDEaAHT40ABxQAPRAF4AfIhkBqAMxdBwsaWIATEBGIp0ECJTDEZUHkpHnVVm5QAMW4SNHAYAGyhksgrKcsoArB463nCkAKJoEAAWQfYyolBwAMpQpDBgAOYoXFoAvkA)**\r\n\r\n**Related Issues:** #36554 collects various issues around array methods.\r\n","author":{"url":"https://github.com/Treora","@type":"Person","name":"Treora"},"datePublished":"2020-05-12T22:50:49Z","url":"https://github.com/microsoft/TypeScript/issues/38514"}
| 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:d601ca42-8bc1-c42b-afb0-1e64373f9a0d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CEF2:23CF06:AF6165:E32DDB:6B00E7A5 |
| html-safe-nonce | 15b9995723599a5814b132d923f7413557be839515650a9419a3419cf5b2c7c0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRUYyOjIzQ0YwNjpBRjYxNjU6RTMyRERCOjZCMDBFN0E1IiwidmlzaXRvcl9pZCI6IjgxMzYwMzkwMjI1NzAwMzkyMDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2b054f9d1b80308164d2eca56c99b46e3efb71f37f29a964289d97dc3690f583 |
| hovercard-subject-tag | issue:617014696 |
| 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/38514/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7059dedd188f60f2d3fbecd9dfba0494534e80261a3244e4cf44232b0ab428dd/microsoft/TypeScript/issues/38514 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7059dedd188f60f2d3fbecd9dfba0494534e80261a3244e4cf44232b0ab428dd/microsoft/TypeScript/issues/38514 |
| og:image:alt | (feel free to improve the issue title) TypeScript Version: 4.0.0-dev.20200512 (also tested on 3.8.3 and 3.9.2) Search Terms: empty tuple union never map reduce filter forEach Code function f(a: [nu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Treora |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| 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 | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width