Title: flatMap errors when callback returns certain union types · Issue #31033 · microsoft/TypeScript · GitHub
Open Graph Title: flatMap errors when callback returns certain union types · Issue #31033 · microsoft/TypeScript
X Title: flatMap errors when callback returns certain union types · Issue #31033 · microsoft/TypeScript
Description: TypeScript Version: 3.4.3 Search Terms: flat, flatmap Code // Map
Open Graph Description: TypeScript Version: 3.4.3 Search Terms: flat, flatmap Code // Map
X Description: TypeScript Version: 3.4.3 Search Terms: flat, flatmap Code // Map<string, number[]> const myMap = new Map([ ['foo', [1, 2, 3]], ['bar', [4, 5, 6]] ]); // string[] const myArra...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/31033
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"flatMap errors when callback returns certain union types","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.4.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:** flat, flatmap\r\n\r\n**Code**\r\n\r\n```ts\r\n// Map\u003cstring, number[]>\r\nconst myMap = new Map([\r\n ['foo', [1, 2, 3]],\r\n ['bar', [4, 5, 6]]\r\n]);\r\n\r\n// string[]\r\nconst myArray = ['foo', 'bar', 'baz'];\r\n\r\n// (key: any) => number[] | undefined\r\nconst mapFn = key => myMap.get(key);\r\n\r\nconst flatMapped = myArray.flatMap(mapFn);\r\n```\r\n\r\n**Actual behavior:**\r\n\r\nWith strict null checks on:\r\n\r\n```\r\nArgument of type '(key: any) => number[] | undefined' is not assignable to parameter of type '(this: undefined, value: string, index: number, array: string[]) => number | readonly number[]'.\r\n Type 'number[] | undefined' is not assignable to type 'number | readonly number[]'.\r\n Type 'undefined' is not assignable to type 'number | readonly number[]'. ts(2345)\r\n```\r\n\r\nTurning strict null checks off prevents the error as you might expect, but that's obviously not desirable.\r\n\r\nMore generally, any callback that returns `foo[] | bar` (where `bar` is not compatible with `foo`) seems to run into the same issue. The `Map.prototype.get` example I've used here just seems like it would be a relatively common use case in particular (you'd typically follow it up by calling `.filter(Boolean)` or similar on the mapped result).\r\n\r\n**More code**\r\n\r\nSeeing if doing the operations separately will help:\r\n\r\n```ts\r\n// (number[] | undefined)[]\r\nconst mapped = myArray.map(mapFn);\r\n\r\n// any[] !!\r\nconst flattened = mapped.flat();\r\n```\r\n\r\n**Expected behavior:**\r\n\r\n`flatMapped`/`flattened` result to be inferred as `(number | undefined)[]`.\r\n\r\n**Playground Link:** \u003c!-- A link to a TypeScript Playground \"Share\" link which demonstrates this behavior -->\r\n\r\nDoesn't seem to be able to load the appropriate library definition, even with `/// \u003creference lib=\"es2019.array\" />`\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\n\r\n#29604 seems to be related to the `.flat()` part.","author":{"url":"https://github.com/noinkling","@type":"Person","name":"noinkling"},"datePublished":"2019-04-19T12:30:32Z","url":"https://github.com/microsoft/TypeScript/issues/31033"}
| 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:f05638cb-4ad5-dc6b-49f5-54bc370f70e4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE66:38E318:73D9A:921F9:6B00F51B |
| html-safe-nonce | 950f80e926408cc07afa0a5c822adcf451bc66b4b2067a28bd68dcd646cb14ed |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTY2OjM4RTMxODo3M0Q5QTo5MjFGOTo2QjAwRjUxQiIsInZpc2l0b3JfaWQiOiI3NzAxMDY5OTI5MTA5MzIwOTg3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 154b1a94152c6266e6a9cc190cae932e50fbdd733a16daf4e4c39ac2c0201822 |
| hovercard-subject-tag | issue:435161931 |
| 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/31033/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1eacb9d4737c848803d378acb22294802c6199fb9046ad3be0cab8c7bbf9b725/microsoft/TypeScript/issues/31033 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1eacb9d4737c848803d378acb22294802c6199fb9046ad3be0cab8c7bbf9b725/microsoft/TypeScript/issues/31033 |
| og:image:alt | TypeScript Version: 3.4.3 Search Terms: flat, flatmap Code // Map |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | noinkling |
| 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