Title: Unable to call map on array of variable depth · Issue #35045 · microsoft/TypeScript · GitHub
Open Graph Title: Unable to call map on array of variable depth · Issue #35045 · microsoft/TypeScript
X Title: Unable to call map on array of variable depth · Issue #35045 · microsoft/TypeScript
Description: I'm trying to write a function which transforms the coordinates of any type of GeoJSON geometry. The coordinates of a Geometry can be either number[] (a Point), number[][] (a LineString), number[][][] (a Polygon) or number[][][][] (a Mul...
Open Graph Description: I'm trying to write a function which transforms the coordinates of any type of GeoJSON geometry. The coordinates of a Geometry can be either number[] (a Point), number[][] (a LineString), number[][...
X Description: I'm trying to write a function which transforms the coordinates of any type of GeoJSON geometry. The coordinates of a Geometry can be either number[] (a Point), number[][] (a LineString), numbe...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/35045
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unable to call map on array of variable depth","articleBody":"\u003c!--\r\nPlease try to reproduce the issue with the latest published version. It may have already been fixed.\r\nFor npm: `typescript@next`\r\nThis is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly\r\n-->\r\n\r\nI'm trying to write a function which transforms the coordinates of any type of GeoJSON geometry. The coordinates of a Geometry can be either `number[]` (a Point), `number[][]` (a LineString), `number[][][]` (a Polygon) or `number[][][][]` (a MultiPolygon). I have a recursive implementation which works but does not type check.\r\n\r\n**TypeScript Version:** 3.7.2\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- 2349\r\n\r\n**Code**\r\n\r\n```ts\r\ntype Position = number[];\r\n\r\nfunction isNestedArray\u003cT extends any[]>(x: T | T[]): x is T[] {\r\n return Array.isArray(x[0]);\r\n}\r\n\r\nexport function transformCoordArray(\r\n coords: Position | Position[] | Position[][],\r\n fn: (pt: Position) => Position\r\n): any[] {\r\n if (isNestedArray(coords)) {\r\n coords; // type is Position[] | Position[][]\r\n return coords.map(c => transformCoordArray(c, fn));\r\n // ~~~ This expression is not callable.\r\n } else {\r\n coords; // type is Position\r\n return fn(coords);\r\n }\r\n}\r\n```\r\n\r\n([playground][bad])\r\n\r\nThe full error message is:\r\n\r\n```\r\nThis expression is not callable.\r\n Each member of the union type '\r\n (\u003cU>(callbackfn: (value: Position, index: number, array: Position[]) => U, thisArg?: any) => U[])\r\n | (\u003cU>(callbackfn: (value: Position[], index: number, array: Position[][]) => U, thisArg?: any) => U[])\r\n' has signatures, but none of those signatures are compatible with each other.(2349)\r\n```\r\n\r\nThe call is valid because `c`'s type should be `Position | Position[]`, which is assignable to `Position | Position[] | Position[][]`.\r\n\r\nInterestingly, I can make the error go away by assigning to a new variable which has a broader type (`(Position | Position[])[]`): \r\n([playground][ok])\r\n\r\n```ts\r\nexport function transformCoordArray(\r\n coords: Position | Position[] | Position[][],\r\n fn: (pt: Position) => Position\r\n): any[] {\r\n if (isNestedArray(coords)) {\r\n coords; // type is Position[] | Position[][]\r\n const c2: (Position | Position[])[] = coords;\r\n return c2.map(c => transformCoordArray(c, fn)); // ok\r\n } else {\r\n coords; // type is Position\r\n return fn(coords);\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nNo error.\r\n\r\n**Actual behavior:**\r\n\r\nThe given error.\r\n\r\n**Playground Link:** [playground][bad]\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\n\r\n\r\n\r\n[bad]: https://www.typescriptlang.org/play/?ssl=14&ssc=55&pln=14&pc=1#code/C4TwDgpgBACg9gZwJbCXAdlAvFdBXAWwCMIAnAbQF0BuAWACgGAzPdAY1QyiQQDkIEwCABMAgqVIBDEAB4AKlAgAPIemEIok9CCoA+ABRKAXFAUAfU1QCUJpdw1yqUAN4MoUUhGB5SmcVJAAOh5-aUNyAAZKKzp6AF8GBmUwOFJgKBZ2TkxgKXQEJlSCAGE4VLEJMLcoNjLSdRN4ZGyoCyaUNHQnNsQOjCoqABpqpnQTfTBgRt7sq2xdWBnOhhtNbSdXenckJih9Hn5BEVCQfVryhCs5zfd3c-qEancAemeoUEh7RebO7u++rqUKjVdyeby+Gp1dSBAiSMBnebvPIFIqlconM6DDLoK6xW5QV74om3AB+ZNMAAseIolGBPAhkFxqeg4Ok2JIADYcyREDkQQLVOKKDkIaA3W73dRPAlvD7QantbIgjxeHyYUZnKGXPFQBLxBhAA\r\n[ok]: https://www.typescriptlang.org/play/?ssl=14&ssc=1&pln=15&pc=1#code/C4TwDgpgBACg9gZwJbCXAdlAvFdBXAWwCMIAnAbQF0BuAWACgGAzPdAY1QyiQQDkIEwCABMAgqVIBDEAB4AKlAgAPIemEIok9CCoA+ABRKAXFAUAfU1QCUJpdw1yqUAN4MoUUhGB5SmcVJAAOh5-aUNyAAZKKzp6AF8GBmUwOFJgKBZ2TkxgKXQEJlSCAGE4VLEJMLcoNjLSdRN4ZGyoCyaUNHQnNsQOjCoqABpqpnQTfTBgRt7sq2xdWBnOhhtNbSdXenckJih9Hn5BEVCQfVryhCs5zfd3c-qEancAemeoUEh7RebO7u++rqUKjVO4YQQ1ABM43aLR6P360ScOHu6lit1unm8vkhgQIkjAZ3m7zyBSKpXKJzOgwy6CuTygrygcAA1tU4ooADYIaA3W4ox4vN4faA8f7ZEEeLw+TCjM51dQxNkMBL0IA","author":{"url":"https://github.com/danvk","@type":"Person","name":"danvk"},"datePublished":"2019-11-11T21:30:09Z","url":"https://github.com/microsoft/TypeScript/issues/35045"}
| 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:67f8adae-566d-623e-d096-d6469cb100cf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8AD8:363D8E:9D21E5:CB28A0:6B00DF60 |
| html-safe-nonce | 4f41da750a2744913167db19b38cfdd7d57177d1012507227e2257301fafa900 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QUQ4OjM2M0Q4RTo5RDIxRTU6Q0IyOEEwOjZCMDBERjYwIiwidmlzaXRvcl9pZCI6IjYwMjU0MDM1MTM5MTAxMjQzODQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4e095a6ee1bd171609a9dd88f97e35d3c899b3476330836dfb423ba7c2f4595d |
| hovercard-subject-tag | issue:521198128 |
| 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/35045/issue_layout |
| twitter:image | https://opengraph.githubassets.com/68120dc56d5edadc67542b76fb853c7dd2d18963e5baccdc97ba91fcf8dfe9dc/microsoft/TypeScript/issues/35045 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/68120dc56d5edadc67542b76fb853c7dd2d18963e5baccdc97ba91fcf8dfe9dc/microsoft/TypeScript/issues/35045 |
| og:image:alt | I'm trying to write a function which transforms the coordinates of any type of GeoJSON geometry. The coordinates of a Geometry can be either number[] (a Point), number[][] (a LineString), number[][... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | danvk |
| 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