Title: Incorrect type inference when using object index · Issue #17011 · microsoft/TypeScript · GitHub
Open Graph Title: Incorrect type inference when using object index · Issue #17011 · microsoft/TypeScript
X Title: Incorrect type inference when using object index · Issue #17011 · microsoft/TypeScript
Description: TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx) 2.4.1 Code function test(paramsObject: { [key: string]: string | Array
Open Graph Description: TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx) 2.4.1 Code function test(paramsObject: { [key: string]: string | Array
X Description: TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx) 2.4.1 Code function test(paramsObject: { [key: string]: string | Array<string> }) { for (let key in paramsObject) { if (paramsObject.h...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/17011
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect type inference when using object index","articleBody":"\u003c!-- BUGS: Please use this template. -->\r\n**TypeScript Version:** 2.4.0 / nightly (2.5.0-dev.201xxxxx)\r\n2.4.1\r\n\r\n**Code**\r\n\r\n```ts\r\nfunction test(paramsObject: { [key: string]: string | Array\u003cstring> }) {\r\n for (let key in paramsObject) {\r\n if (paramsObject.hasOwnProperty(key)) {\r\n if (Array.isArray(paramsObject[key])) {\r\n doArray(paramsObject[key]);\r\n } else {\r\n doString(paramsObject[key]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n\r\nfunction doString(s: string) {\r\n console.log(s);\r\n}\r\n\r\nfunction doArray\u003cT>(a: Array\u003cT>) {\r\n console.log(a);\r\n}\r\n\r\n```\r\n\r\n**Expected behavior:**\r\nTypescript should be able to infer that `paramsObject[key]` is still a string or string array after `Array.isArray` has been called. It can't change. \r\nIt works correct if I assign `paramsObject[key]` to a variable beforehand, and then use that variable in both the check and the `doArray` and `doString`. \r\n\r\n**Actual behavior:**\r\n`Argument of type 'string | string[]' is not assignable to parameter of type 'string[]'` for `doArray`\r\nand\r\n`Argument of type 'string | string[]' is not assignable to parameter of type 'string'.` for `doString`","author":{"url":"https://github.com/zlepper","@type":"Person","name":"zlepper"},"datePublished":"2017-07-07T15:37:31Z","url":"https://github.com/microsoft/TypeScript/issues/17011"}
| 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:5b66087d-3034-e12d-874d-427bef605872 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EE2A:2D0B15:47D90A:62CB8E:6B047001 |
| html-safe-nonce | 09f7106e38ef4b41d3c5e3662f61df2a85b1ab6368a8a77cfaf174a4a34ab1d7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRTJBOjJEMEIxNTo0N0Q5MEE6NjJDQjhFOjZCMDQ3MDAxIiwidmlzaXRvcl9pZCI6IjkxNjUxODEzOTkzNjI0NjU3OTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3316f2a43a5650a696ff6a4340ed384439bdffe002e1775f8ab4cff187afc94f |
| hovercard-subject-tag | issue:241301755 |
| 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/17011/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e3227358862d6f4a802812002c027231e4f70542fc7476da88f01761581ed438/microsoft/TypeScript/issues/17011 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e3227358862d6f4a802812002c027231e4f70542fc7476da88f01761581ed438/microsoft/TypeScript/issues/17011 |
| og:image:alt | TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx) 2.4.1 Code function test(paramsObject: { [key: string]: string | Array |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zlepper |
| hostname | github.com |
| expected-hostname | github.com |
| None | ae8802492f12d3ba4f6b0d814054e617a2a5616b6327bf8555c10cd5b0dc2a6f |
| 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 | cfb5f24c6e01d85f70804652a5107e5cac03a161 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width