Title: Control flow analysis overriding an explicit type declaration when the type flows from optional chaining · Issue #50444 · microsoft/TypeScript · GitHub
Open Graph Title: Control flow analysis overriding an explicit type declaration when the type flows from optional chaining · Issue #50444 · microsoft/TypeScript
X Title: Control flow analysis overriding an explicit type declaration when the type flows from optional chaining · Issue #50444 · microsoft/TypeScript
Description: Bug Report I'm not exactly sure if this is a bug or not - CFA rules are not well-documented. I find the present behavior to be quite annoying though 🔎 Search Terms narrowing, control flow analysis, nullable, optional chaining 🕗 Version &...
Open Graph Description: Bug Report I'm not exactly sure if this is a bug or not - CFA rules are not well-documented. I find the present behavior to be quite annoying though 🔎 Search Terms narrowing, control flow analysis,...
X Description: Bug Report I'm not exactly sure if this is a bug or not - CFA rules are not well-documented. I find the present behavior to be quite annoying though 🔎 Search Terms narrowing, control flow analy...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/50444
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Control flow analysis overriding an explicit type declaration when the type flows from optional chaining","articleBody":"# Bug Report\r\n\r\nI'm not exactly sure if this is a bug or not - CFA rules are not well-documented. I find the present behavior to be quite annoying though\r\n\r\n### 🔎 Search Terms\r\n\r\nnarrowing, control flow analysis, nullable, optional chaining\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n\u003c!-- When did you start seeing this bug occur?\r\n\r\n\"Bugs\" that have existed in TS for a long time are very likely to be FAQs; refer to\r\n https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs\r\n\r\nIf possible, please try testing the nightly version of TS to see if it's already been fixed.\r\nFor npm: `typescript@next`\r\nThis is also the 'Nightly' version in the playground: [TS nightly playground](https://www.typescriptlang.org/play?ts=4.9.0-dev.20220825#code/PTAEAcFcCdwewM4FMBmkA26CepLNAAYACALluEgsAFQGgAWS0SoARkgMYCGeLcJjaAHcAlvgCSDLgBMI0OK3RIAtglCiBoAIIAVLaABQI5fGglQAclLlKwAG4IOcaUgC0QpKzsikQiwYMnADsEcwcnFwAuUAAlJBIYIJ0bAB4yCjgUUC4OAEdIEWYANQQAYWckLXARAD5QAB9cIJcUESCkWQBebLyC4rKKqpEAfgA6AAoASgMQUDnQAD1hgPCKmbAlgNmuEhIVcHMSOHVoLnBQNuyg0HFxADEAUUC4ELDHCoAmaLiE6CTU9JITI9fKFJAlcouIZ1RqQZqoNodUDdcZTZF1HKg-qQyrVMZTSZTADc63mi2WpKCxw49C4QQA5iwBHBIPT6AZVi4PqTNs9XqBOUgAMzfeKJZIUNI2YGYvrggZQ6owpotRFdEFyiGDaqgYYasFaxUiNHROGq9rSEmzea82atIJcTA4QA8G4BI-Y57xcQp5wyAA)\r\n\r\n- This is the behavior in every version (since 3.7 as that's the version that introduced optional chaining) I tried\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.7.4#code/PTAEAcFcCdwewM4FMBmkA26CepLNAAYACALluEgsAFQGgAWS0SoARkgMYCGeLcJjaAHcAlvgCSDLgBMI0OK3RIAtglCiBoAIIAVLaABQI5fGglQAclLlKwAG4IOcaUgC0QpKzsikQiwYMnADsEcwcnFwAuUAAlJBIYIJ0bAB4yCjgUUC4OAEdIEWYANQQAYWckLXARAD5QAB9cIJcUESCkWQBebLyC4rKKqpEAfgA6AAoASgMQUDnQAD1hgPCKmbAlgNmuEhIVcHMSOHVoLnBQNuyg0HFxADEAUUC4ELDHCoAmaLiE6CTU9JITI9fKFJAlcouIZ1RqQZqoNodUDdcZTZF1HKg-qQyrVMZTSZTADc63mi2WpKCxw49C4QQA5iwBHBIPT6AZVi4PqTNs9XqBOUgAMzfeKJZIUNI2YGYvrggZQ6owpotRFdEFyiGDaqgYYasFaxUiNHROGq9rSEmzea82atIJcTA4QA8G4BI-Y57xcQp5wyAA)\r\n\r\n### 💻 Code\r\n\r\n\u003c!-- Please post the relevant code sample here as well-->\r\n```ts\r\n// purposefully use `@types/*` here because otherwise I had problems with ATA \r\nimport '@types/vscode-webview'\r\n\r\nconst vscode: ReturnType\u003ctypeof acquireVsCodeApi> | undefined = acquireVsCodeApi?.()\r\n// ^? WebviewApi\u003cunknown> | undefined\r\n\r\nvscode\r\n// ^? WebviewApi\u003cunknown>\r\n\r\n// attempt to wrap in an IIFE\r\nconst vscode2: ReturnType\u003ctypeof acquireVsCodeApi> | undefined = (() => acquireVsCodeApi?.())();\r\n// ^? WebviewApi\u003cunknown> | undefined\r\n\r\n// no change though\r\nvscode2\r\n// ^? WebviewApi\u003cunknown>\r\n\r\nconst vscode3: ReturnType\u003ctypeof acquireVsCodeApi> | undefined = acquireVsCodeApi ? acquireVsCodeApi() : undefined;\r\n// ^? WebviewApi\u003cunknown> | undefined\r\n\r\n// finally 🎉\r\nvscode3\r\n// ^? WebviewApi\u003cunknown> | undefined\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nReferencing a variable with the declared type that originates in an expression using optional chaining doesn't include `| undefined` in its type. \r\n\r\nNote that `acquireVsCodeApi` is **always** defined according to the types.\r\n\r\n### 🙂 Expected behavior\r\n\r\nEven though `acquireVsCodeApi` is always defined I would expect my explicit declaration on the assigned variable to win for the purpose of CFA.\r\n\r\nI have an app that is running as a standalone but it's also bundled as a VS Code extension. It's a hussle to include the VS Code types conditionally and it's much easier for us to just add those defs globally and refine this stuff at runtime when needed.\r\n","author":{"url":"https://github.com/Andarist","@type":"Person","name":"Andarist"},"datePublished":"2022-08-25T13:11:52Z","url":"https://github.com/microsoft/TypeScript/issues/50444"}
| 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:57137baa-98d0-da7a-c65a-d533aa12d7dc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 928C:3F4253:27B2D47:34E4BA3:6B01B129 |
| html-safe-nonce | e2622af28bb20890625e5994f05e041ed6ab00dab9137131a70644388511becf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MjhDOjNGNDI1MzoyN0IyRDQ3OjM0RTRCQTM6NkIwMUIxMjkiLCJ2aXNpdG9yX2lkIjoiOTE5Njg0MjgyODgyMTU0MTE2MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9dbf7f8d01819a6d69409fc10352e930e7acd7e0a3fa5a9954a41cd68a3ab0e3 |
| hovercard-subject-tag | issue:1350878660 |
| 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/50444/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ec315f46a0f0550d47ca7794b303abceee07381088df19edecb898f9eb5e09f2/microsoft/TypeScript/issues/50444 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ec315f46a0f0550d47ca7794b303abceee07381088df19edecb898f9eb5e09f2/microsoft/TypeScript/issues/50444 |
| og:image:alt | Bug Report I'm not exactly sure if this is a bug or not - CFA rules are not well-documented. I find the present behavior to be quite annoying though 🔎 Search Terms narrowing, control flow analysis,... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Andarist |
| 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 | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width