{"id":4902,"date":"2023-01-05T11:15:53","date_gmt":"2023-01-05T03:15:53","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4902"},"modified":"2023-01-05T11:15:53","modified_gmt":"2023-01-05T03:15:53","slug":"%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84%e9%a1%9e%e5%9e%8b%e5%ae%88%e8%a1%9b%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%e9%a1%9e%e5%9e%8b%e5%ae%88%e8%a1%9b%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u4e86\u89e3TypeScript\u4e2d\u7684\u985e\u578b\u5b88\u885b\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u985e\u578b\u5b88\u885b, Type Guards\"><\/p>\n<h1>\u4ec0\u9ebc\u662f TypeScript &#8220;\u985e\u578b\u5b88\u885b(Type Guards)&#8221;<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u985e\u578b\u6aa2\u67e5\u529f\u80fd\uff0c\u53ef\u4ee5\u5728\u7de8\u8b6f\u6642\u671f\u6aa2\u67e5\u51fa\u7a0b\u5f0f\u78bc\u4e2d\u7684\u932f\u8aa4\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u54c1\u8cea\u3002TypeScript \u4e2d\u7684\u985e\u578b\u5b88\u885b\uff08Type Guards\uff09\u662f\u4e00\u7a2e\u7279\u6b8a\u7684\u8a9e\u6cd5\uff0c\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u8b8a\u6578\u7684\u985e\u578b\uff0c\u4e26\u57fa\u65bc\u6aa2\u67e5\u7d50\u679c\u505a\u51fa\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<h2>\u985e\u578b\u5b88\u885b\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u985e\u578b\u5b88\u885b\u662f\u4e00\u7a2e\u7279\u6b8a\u7684\u8a9e\u6cd5\uff0c\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u8b8a\u6578\u7684\u985e\u578b\uff0c\u4e26\u57fa\u65bc\u6aa2\u67e5\u7d50\u679c\u505a\u51fa\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u5b83\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u8b8a\u6578\u662f\u5426\u70ba\u7279\u5b9a\u7684\u985e\u578b\uff0c\u4f8b\u5982\u5b57\u4e32\u3001\u6578\u5b57\u3001\u5e03\u6797\u503c\u7b49\uff0c\u4e26\u57fa\u65bc\u6aa2\u67e5\u7d50\u679c\u505a\u51fa\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u8209\u500b\u4f8b\u5b50\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u8b8a\u6578 <code>x<\/code>\uff0c\u5b83\u53ef\u4ee5\u662f\u5b57\u4e32\u6216\u6578\u5b57\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u985e\u578b\u5b88\u885b\u4f86\u6aa2\u67e5 <code>x<\/code> \u7684\u985e\u578b\uff0c\u4e26\u57fa\u65bc\u6aa2\u67e5\u7d50\u679c\u505a\u51fa\u4e0d\u540c\u7684\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-typescript line-numbers\">let x: string | number;\n\nif (typeof x === 'string') {\n  \/\/ x \u662f\u5b57\u4e32\uff0c\u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n} else if (typeof x === 'number') {\n  \/\/ x \u662f\u6578\u5b57\uff0c\u57f7\u884c\u53e6\u4e00\u4e9b\u64cd\u4f5c\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>typeof<\/code> \u904b\u7b97\u5b50\u4f86\u6aa2\u67e5 <code>x<\/code> \u7684\u985e\u578b\uff0c\u4e26\u57fa\u65bc\u6aa2\u67e5\u7d50\u679c\u505a\u51fa\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<h2>\u985e\u578b\u5b88\u885b\u7684\u512a\u9ede<\/h2>\n<p>\u985e\u578b\u5b88\u885b\u7684\u6700\u5927\u512a\u9ede\u662f\u53ef\u4ee5\u6e1b\u5c11\u7a0b\u5f0f\u78bc\u4e2d\u7684\u932f\u8aa4\uff0c\u56e0\u70ba\u5b83\u53ef\u4ee5\u5728\u7de8\u8b6f\u6642\u671f\u6aa2\u67e5\u51fa\u7a0b\u5f0f\u78bc\u4e2d\u7684\u932f\u8aa4\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u54c1\u8cea\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u985e\u578b\u5b88\u885b\u4e5f\u53ef\u4ee5\u6e1b\u5c11\u7a0b\u5f0f\u78bc\u7684\u8907\u96dc\u5ea6\uff0c\u56e0\u70ba\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u5728\u4e00\u500b\u5730\u65b9\u6aa2\u67e5\u8b8a\u6578\u7684\u985e\u578b\uff0c\u800c\u4e0d\u9700\u8981\u5728\u6bcf\u500b\u5730\u65b9\u90fd\u6aa2\u67e5\u8b8a\u6578\u7684\u985e\u578b\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>TypeScript \u4e2d\u7684\u985e\u578b\u5b88\u885b\uff08Type Guards\uff09\u662f\u4e00\u7a2e\u7279\u6b8a\u7684\u8a9e\u6cd5\uff0c\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u8b8a\u6578\u7684\u985e\u578b\uff0c\u4e26\u57fa\u65bc\u6aa2\u67e5\u7d50\u679c\u505a\u51fa\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u5b83\u53ef\u4ee5\u6e1b\u5c11\u7a0b\u5f0f\u78bc\u4e2d\u7684\u932f\u8aa4\uff0c\u4e26\u6e1b\u5c11\u7a0b\u5f0f\u78bc\u7684\u8907\u96dc\u5ea6\uff0c\u662f\u4e00\u7a2e\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:TypeScript\u4e2d\u7684\u985e\u578b\u5b88\u885b\u662f\u4e00\u7a2e\u6280\u8853\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7ba1\u7406\u548c\u63a7\u5236\u7a0b\u5e8f\u7684\u985e\u578b\u3002\u672c\u6587\u5c07\u4ecb\u7d39TypeScript\u4e2d\u7684\u985e\u578b\u5b88\u885b\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u6280\u5de7\uff0c\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u5229\u7528\u5b83\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-4902","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-1h4","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4902","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=4902"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4902\/revisions"}],"predecessor-version":[{"id":4903,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4902\/revisions\/4903"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}