{"id":4896,"date":"2023-01-05T11:14:36","date_gmt":"2023-01-05T03:14:36","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4896"},"modified":"2023-01-05T11:14:36","modified_gmt":"2023-01-05T03:14:36","slug":"%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84%e4%ba%a4%e5%8f%89%e9%a1%9e%e5%9e%8bintersectiontypes%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84%e4%ba%a4%e5%8f%89%e9%a1%9e%e5%9e%8bintersectiontypes%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u4e86\u89e3TypeScript\u4e2d\u7684\u4ea4\u53c9\u985e\u578b(IntersectionTypes)\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u4ea4\u53c9\u985e\u578b, Intersection Types\"><\/p>\n<h1>\u4ec0\u9ebc\u662f TypeScript \u4ea4\u53c9\u985e\u578b(Intersection Types)<\/h1>\n<p>TypeScript \u4ea4\u53c9\u985e\u578b(Intersection Types)\u662f\u4e00\u7a2e\u7d44\u5408\u591a\u500b\u985e\u578b\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c07\u591a\u500b\u985e\u578b\u7684\u7279\u6027\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u5275\u9020\u51fa\u4e00\u500b\u65b0\u7684\u985e\u578b\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u4ea4\u53c9\u985e\u578b\u7684\u8a9e\u6cd5<\/h2>\n<p>\u4ea4\u53c9\u985e\u578b\u7684\u8a9e\u6cd5\u5f88\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u4f7f\u7528 <code>&amp;<\/code> \u7b26\u865f\u5c07\u591a\u500b\u985e\u578b\u7d44\u5408\u5728\u4e00\u8d77\u5373\u53ef\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-typescript line-numbers\">type A = { a: number };\ntype B = { b: string };\n\ntype AandB = A &amp; B;\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u5169\u500b\u985e\u578b <code>A<\/code> \u548c <code>B<\/code>\uff0c\u7136\u5f8c\u4f7f\u7528 <code>&amp;<\/code> \u7b26\u865f\u5c07\u5b83\u5011\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u5275\u9020\u51fa\u65b0\u7684\u985e\u578b <code>AandB<\/code>\u3002<\/p>\n<h2>\u4ea4\u53c9\u985e\u578b\u7684\u7279\u6027<\/h2>\n<p>\u4ea4\u53c9\u985e\u578b\u7684\u7279\u6027\u662f\uff0c\u5b83\u53ef\u4ee5\u5c07\u591a\u500b\u985e\u578b\u7684\u7279\u6027\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u5275\u9020\u51fa\u4e00\u500b\u65b0\u7684\u985e\u578b\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u4ee5\u5c07 <code>A<\/code> \u548c <code>B<\/code> \u985e\u578b\u7684\u7279\u6027\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u5275\u9020\u51fa <code>AandB<\/code> \u985e\u578b\uff0c\u5b83\u5177\u6709 <code>A<\/code> \u548c <code>B<\/code> \u985e\u578b\u7684\u6240\u6709\u7279\u6027\uff1a<\/p>\n<pre><code class=\"language-typescript line-numbers\">type A = { a: number };\ntype B = { b: string };\n\ntype AandB = A &amp; B;\n\nconst obj: AandB = {\n  a: 1,\n  b: 'foo'\n};\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b <code>AandB<\/code> \u985e\u578b\uff0c\u5b83\u5177\u6709 <code>A<\/code> \u548c <code>B<\/code> \u985e\u578b\u7684\u6240\u6709\u7279\u6027\uff0c\u6211\u5011\u53ef\u4ee5\u5c07 <code>a<\/code> \u548c <code>b<\/code> \u5c6c\u6027\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u5275\u9020\u51fa\u4e00\u500b\u65b0\u7684\u5c0d\u8c61\u3002<\/p>\n<h2>\u4ea4\u53c9\u985e\u578b\u7684\u512a\u9ede<\/h2>\n<p>\u4ea4\u53c9\u985e\u578b\u7684\u512a\u9ede\u662f\uff0c\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u5c07\u591a\u500b\u985e\u578b\u7684\u7279\u6027\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u5275\u9020\u51fa\u4e00\u500b\u65b0\u7684\u985e\u578b\uff0c\u9019\u6a23\u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u52a0\u9748\u6d3b\u5730\u4f7f\u7528 TypeScript\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u4ea4\u53c9\u985e\u578b\u4e5f\u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u52a0\u5bb9\u6613\u5730\u5c07\u4e0d\u540c\u985e\u578b\u7684\u5c0d\u8c61\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u4ee5\u5c07 <code>A<\/code> \u548c <code>B<\/code> \u985e\u578b\u7684\u5c0d\u8c61\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u5275\u9020\u51fa\u4e00\u500b\u65b0\u7684\u5c0d\u8c61\uff1a<\/p>\n<pre><code class=\"language-typescript line-numbers\">type A = { a: number };\ntype B = { b: string };\n\ntype AandB = A &amp; B;\n\nconst objA: A = { a: 1 };\nconst objB: B = { b: 'foo' };\n\nconst obj: AandB = { ...objA, ...objB };\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b <code>AandB<\/code> \u985e\u578b\uff0c\u7136\u5f8c\u5c07 <code>objA<\/code> \u548c <code>objB<\/code> \u5c0d\u8c61\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u5275\u9020\u51fa\u4e00\u500b\u65b0\u7684\u5c0d\u8c61 <code>obj<\/code>\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>TypeScript \u4ea4\u53c9\u985e\u578b(Intersection Types)\u662f\u4e00\u7a2e\u7d44\u5408\u591a\u500b\u985e\u578b\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c07\u591a\u500b\u985e\u578b\u7684\u7279\u6027\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u5275\u9020\u51fa\u4e00\u500b\u65b0\u7684\u985e\u578b\u3002\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u52a0\u9748\u6d3b\u5730\u4f7f\u7528 TypeScript\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u52a0\u5bb9\u6613\u5730\u5c07\u4e0d\u540c\u985e\u578b\u7684\u5c0d\u8c61\u7d44\u5408\u5728\u4e00\u8d77\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):TypeScript\u4e2d\u7684\u4ea4\u53c9\u985e\u578b(IntersectionTypes)\u53ef\u4ee5\u5c07\u591a\u500b\u985e\u578b\u5408\u4f75\u70ba\u4e00\u500b\u985e\u578b\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u7684\u4f7f\u7528TypeScript\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528\u4ea4\u53c9\u985e\u578b(IntersectionTypes)\u4f86\u63d0\u5347\u958b\u767c\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-4896","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1gY","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=4896"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4896\/revisions"}],"predecessor-version":[{"id":4897,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4896\/revisions\/4897"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}