Title: Assingment type inference is broken with map, forEach · Issue #61755 · microsoft/TypeScript · GitHub
Open Graph Title: Assingment type inference is broken with map, forEach · Issue #61755 · microsoft/TypeScript
X Title: Assingment type inference is broken with map, forEach · Issue #61755 · microsoft/TypeScript
Description: 🔎 Search Terms "union narrowing", "map", "forEach" 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about type inference and narrowing ⏯ Playground Link https://www.type...
Open Graph Description: 🔎 Search Terms "union narrowing", "map", "forEach" 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about type inference and narr...
X Description: 🔎 Search Terms "union narrowing", "map", "forEach" 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for ent...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/61755
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Assingment type inference is broken with map, forEach","articleBody":"### 🔎 Search Terms\n\n\"union narrowing\", \"map\", \"forEach\"\n\n### 🕗 Version & Regression Information\n\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about type inference and narrowing\n\n### ⏯ Playground Link\n\nhttps://www.typescriptlang.org/play/?ts=3.3.3#code/DYUwLgBAhgXBB2BXAtgIxAJwgHwY4wEAvHgQNwBQFUlFA2gIwA0EATCwMwC6AdAGYB7DAFEoAYwAWACnjEAfBADeFCKujEIDSgF8AlFRpVBWKWIHwAzpAAeEAXwiMm7brqUq1UDVoraDlIA\n\n### 💻 Code\n\n```ts\nlet a: number | null = null;\n\na;\n\n[1, 2, 3].forEach(n => {\n a = 1;\n})\n\na;\n\nfor (const x of [1,2,3]) {\n a = 1;\n}\n\na;\n```\n\n\n### 🙁 Actual behavior\n\nVariable `a` on line 9 is inferred as `null` even though it is assigned a number value in `forEach`\n\n### 🙂 Expected behavior\n\nExpect it to be inferred as `number | null` or preferably `number`.\n\nOn line 15 it is demonstrated that after `for (const x of xs) { a = 1; }` the type of `a` is inferred as `number | null`. I would expect `map` and `forEach` to follow the same logic.\n\n### Additional information about the issue\n\nThis can be worked around by defining `a` on line 1 like this\n```ts\nconst a = null as null | number;\n```\nNow `a` is inferred as `number | null` in both cases","author":{"url":"https://github.com/pakettiale","@type":"Person","name":"pakettiale"},"datePublished":"2025-05-22T19:52:14Z","url":"https://github.com/microsoft/TypeScript/issues/61755"}
| 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:df7f07a5-a54e-a54c-4130-4e5310a369c7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 826A:4279A:23FDF5D:307129E:6B01AB42 |
| html-safe-nonce | 887e2e571080c4bd13b69d337b7b36d51a3d37fafd33e431a69922c81de5176f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MjZBOjQyNzlBOjIzRkRGNUQ6MzA3MTI5RTo2QjAxQUI0MiIsInZpc2l0b3JfaWQiOiIxNTYyMDM0MDYzNTE3MjY0NzA2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 63ca308c537938bf6c6059274322e32e1e0ab44375ed44891aeced6baa04523d |
| hovercard-subject-tag | issue:3084407164 |
| 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/61755/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5c7741b7004f193d99fc856acbb037e095d30df509475dd42cfb07542f61811a/microsoft/TypeScript/issues/61755 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5c7741b7004f193d99fc856acbb037e095d30df509475dd42cfb07542f61811a/microsoft/TypeScript/issues/61755 |
| og:image:alt | 🔎 Search Terms "union narrowing", "map", "forEach" 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about type inference and narr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pakettiale |
| 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 | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width