Title: Property type update not continued inside next block after checking · Issue #48183 · microsoft/TypeScript · GitHub
Open Graph Title: Property type update not continued inside next block after checking · Issue #48183 · microsoft/TypeScript
X Title: Property type update not continued inside next block after checking · Issue #48183 · microsoft/TypeScript
Description: Bug Report When an object contains an optional property that could either be an array or undefined. Checking that property exists as an array changes its type to only an array instead of optional. Later on, inside a .some of another arra...
Open Graph Description: Bug Report When an object contains an optional property that could either be an array or undefined. Checking that property exists as an array changes its type to only an array instead of optional. ...
X Description: Bug Report When an object contains an optional property that could either be an array or undefined. Checking that property exists as an array changes its type to only an array instead of optional. ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/48183
X: @github
Domain: github.com
IPv4 address: 140.82.113.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Property type update not continued inside next block after checking","articleBody":"# Bug Report\r\n\r\nWhen an object contains an optional property that could either be an array or undefined. Checking that property exists as an array changes its type to only an array instead of optional. Later on, inside a `.some` of another array, the first array goes back to being possibly undefined. If I destructure the item before checking it's value, ts works perfectly but the destructure would be unnecessary in this case.\r\n\r\n### 🔎 Search Terms\r\n\r\n\"Object is possibly 'undefined'.\"\r\n\"type guard not applied\"\r\n\"possibly undefined inside method\"\r\n\r\n### 🕗 Version & Regression Information\r\n\r\nThis is the behavior in every version of the playground and could not find anything in the FAQ's that related.\r\nI did find this issue but I'm unsure if it's related - https://github.com/microsoft/TypeScript/issues/10530\r\n\r\n### ⏯ Playground Link\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgKoGdrIN4FgBQyyADtALbDrrAD2I6A-AFzLphSgDmA2gLoDcBAL4ECMAK4gEYWiGQIAFhAQBrAArlK1OugAUBIuMxQWGaABoDyKBACO44DYAmGqBSqzGLNhxA9elvgAlDhWAPRhyAByNCSaHjrIcDbWdg7OVsAwyLoAhDb2jhAu8dr0DAB0ADYQfmAKITZg4lBy7OIQgvjhkWZQyApw6MggsaRuWp7IAEbiYHETCfRJKQXpxZkweQCCUFBwAJ4VlLv7B7pG0BXj7mXoIQA+D8i5l1DXpZ7VtZz1IVZEJotOTwKqYLo9ZDbeY1IbzOgoNZFJwLW6yZBkIzzaYoMhwMCKDaEVLNVqpQrOVxonQVdA0MgQXRIymfOjIAC8AD5QsSiBEoXJqH4aqjJmyEHA5DiMfjCU4AcgsjlmcUqWK5OzNcgAOSQNjakJ4XlEEnA5BvD6LO7HKRVcROCB6FUlK2yIJdE3IETdY1Asmg8FWITu4RAA)\r\n\r\n### 💻 Code\r\n\r\n\u003c!-- Please post the relevant code sample here as well-->\r\n```ts\r\ninterface User {\r\n permissions?: string[];\r\n}\r\n\r\nfunction checkPermissions(\r\n user: User,\r\n requiredPermissions?: string[],\r\n) {\r\n if (!requiredPermissions?.length) return true;\r\n\r\n // destructure user.permissions here fixes the errors\r\n // const { permissions } = user; \r\n\r\n if(!Array.isArray(user.permissions) || !user.permissions.length) \r\n return false;\r\n\r\n return requiredPermissions.some(requiredPermission => {\r\n if (requiredPermission === 'test') {\r\n return user.permissions.includes(requiredPermission);\r\n }\r\n\r\n return false;\r\n });\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nError: Object is possibly 'undefined'. \r\nAbove error is shown for user.permissions inside the `some` function. \r\n\r\n### 🙂 Expected behavior\r\n\r\nThe `user.permissions` object should be an array inside the some. It is an array in the second condition of the if statement above it.\r\n","author":{"url":"https://github.com/silverlight513","@type":"Person","name":"silverlight513"},"datePublished":"2022-03-09T11:16:32Z","url":"https://github.com/microsoft/TypeScript/issues/48183"}
| 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:d8a7f2be-cecb-e44d-0073-b17982506f0e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 93D6:3F9CD1:67D3A9:893EAD:6B04C582 |
| html-safe-nonce | 8c3462a27c32e220de99988996819969083e76c29a9d34206e2145d1a292bbf4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5M0Q2OjNGOUNEMTo2N0QzQTk6ODkzRUFEOjZCMDRDNTgyIiwidmlzaXRvcl9pZCI6Ijg4MjI3NzA5MzQxODk3MDQ1NzgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ca5e7fdf76e725aede709610409fece7320f9c83074a9f1a9cdb7c86fb7b7e88 |
| hovercard-subject-tag | issue:1163806724 |
| 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/48183/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4cb6c508c11ff8fa5a148da24710ce783b0b9f77aa1405d724a94e64a336ef44/microsoft/TypeScript/issues/48183 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4cb6c508c11ff8fa5a148da24710ce783b0b9f77aa1405d724a94e64a336ef44/microsoft/TypeScript/issues/48183 |
| og:image:alt | Bug Report When an object contains an optional property that could either be an array or undefined. Checking that property exists as an array changes its type to only an array instead of optional. ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | silverlight513 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 85dd75377eb77313206b8fc108bcee6a3a029286d94bf32bf9b4426e4d92e0c4 |
| 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 | 0d64fa748a2bab9a5b587a967ac4da1cfa8da6dd |
| 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