Title: Property goes undetermined on discriminated union · Issue #9977 · microsoft/TypeScript · GitHub
Open Graph Title: Property goes undetermined on discriminated union · Issue #9977 · microsoft/TypeScript
X Title: Property goes undetermined on discriminated union · Issue #9977 · microsoft/TypeScript
Description: TypeScript Version: 2.0 beta Code "use strict"; type IDLMemberTypes = OperationMemberType | ConstantMemberType; interface IDLTypeDescription { origin: string; } interface InterfaceType { members: IDLMemberTypes[]; } interface OperationMe...
Open Graph Description: TypeScript Version: 2.0 beta Code "use strict"; type IDLMemberTypes = OperationMemberType | ConstantMemberType; interface IDLTypeDescription { origin: string; } interface InterfaceType { members: I...
X Description: TypeScript Version: 2.0 beta Code "use strict"; type IDLMemberTypes = OperationMemberType | ConstantMemberType; interface IDLTypeDescription { origin: string; } interface InterfaceType { ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/9977
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Property goes undetermined on discriminated union","articleBody":"**TypeScript Version:** 2.0 beta\n\n**Code**\n\n``` ts\n\"use strict\";\n\ntype IDLMemberTypes = OperationMemberType | ConstantMemberType;\n\ninterface IDLTypeDescription {\n origin: string;\n}\n\ninterface InterfaceType {\n members: IDLMemberTypes[];\n}\n\ninterface OperationMemberType {\n type: \"operation\";\n idlType: IDLTypeDescription | null;\n}\n\ninterface ConstantMemberType {\n type: \"const\";\n idlType: string;\n}\n```\n\n``` ts\n// case 1\nfunction insertInterface(callbackType: InterfaceType) {\n for (const memberType of callbackType.members) {\n if (memberType.type === \"const\") {\n memberType.idlType\n // type of idlType: string, no error\n }\n else if (memberType.type === \"operation\") {\n memberType.idlType.origin;\n // type of idlType: IDLTypeDescription at least on mouse hover\n /*\n Mutually incompatible messages appear simultaneously:\n\n 1. (property) IDLTypeDescription.origin: string\n 2. Property 'origin' does not exist on type 'string'\n */\n (memberType.idlType as IDLTypeDescription);\n // Error: Type 'string' cannot be converted to type 'IDLTypeDescription'\n }\n }\n}\n```\n\n``` ts\n// case 2\nfunction insertInterface2(callbackType: InterfaceType) {\n for (const memberType of callbackType.members) {\n if (memberType.type === \"operation\") {\n memberType.idlType.origin\n // type of idlType: IDLTypeDescription\n // type of origin: string, no error\n }\n }\n}\n```\n\n``` ts\n// case 3\nfunction foo(memberType: IDLMemberTypes) {\n if (memberType.type === \"const\") {\n memberType.idlType\n // type of idlType: string, no error\n }\n else if (memberType.type === \"operation\") {\n memberType.idlType.origin\n // type of idlType: IDLTypeDescription\n // type of origin: string, no error\n }\n}\n```\n\n**Expected behavior:**\n\n`memberType.idlType.origin` should be string in both case thanks to the type guard\n\n**Actual behavior:**\n\nType guard confuses the type of idlType\n","author":{"url":"https://github.com/saschanaz","@type":"Person","name":"saschanaz"},"datePublished":"2016-07-27T03:04:40Z","url":"https://github.com/microsoft/TypeScript/issues/9977"}
| 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:4785ac09-455e-f34a-0ea8-4d8dae619835 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EBE2:1F7384:1086CA3:15E1609:6B010E78 |
| html-safe-nonce | f5b94ff0209288f704633a4cda2f56b1922476db5ee7576656cdee12e3661783 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQkUyOjFGNzM4NDoxMDg2Q0EzOjE1RTE2MDk6NkIwMTBFNzgiLCJ2aXNpdG9yX2lkIjoiOTc3MjU5NzU3NzY3Mjk4NjgxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e33ad232c5736b7a233b85019deeffb8cfb3eb3be1950090f395da92f28faf83 |
| hovercard-subject-tag | issue:167759372 |
| 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/9977/issue_layout |
| twitter:image | https://opengraph.githubassets.com/949a10b92bf9d2e785ab18e63c04fe973ee19b3db592a3fdc290b7c922e64b17/microsoft/TypeScript/issues/9977 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/949a10b92bf9d2e785ab18e63c04fe973ee19b3db592a3fdc290b7c922e64b17/microsoft/TypeScript/issues/9977 |
| og:image:alt | TypeScript Version: 2.0 beta Code "use strict"; type IDLMemberTypes = OperationMemberType | ConstantMemberType; interface IDLTypeDescription { origin: string; } interface InterfaceType { members: I... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | saschanaz |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width