{"id":4870,"date":"2023-01-05T11:09:27","date_gmt":"2023-01-05T03:09:27","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4870"},"modified":"2023-01-05T11:09:27","modified_gmt":"2023-01-05T03:09:27","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8typescript%e7%b1%bb%e5%9e%8b%e5%ae%88%e5%8d%ab%e5%87%bd%e6%95%b0%e8%87%aa%e5%ae%9a%e4%b9%89","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8typescript%e7%b1%bb%e5%9e%8b%e5%ae%88%e5%8d%ab%e5%87%bd%e6%95%b0%e8%87%aa%e5%ae%9a%e4%b9%89\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u5229\u7528TypeScript\u7c7b\u578b\u5b88\u536b\u51fd\u6570\u81ea\u5b9a\u4e49"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, Type Guards, \u7c7b\u578b\u5b88\u536b\u51fd\u6570\"><\/p>\n<h1>\u5229\u7528 TypeScript \u7c7b\u578b\u5b88\u536b\u51fd\u6570(type guards)\u7684\u81ea\u5b9a\u4e49<\/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\u932f\u8aa4\uff0c\u63d0\u9ad8\u958b\u767c\u6548\u7387\u3002TypeScript \u4e2d\u7684\u7c7b\u578b\u5b88\u536b\u51fd\u6570(type guards)\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u8b8a\u91cf\u7684\u985e\u578b\uff0c\u4ee5\u78ba\u4fdd\u7a0b\u5e8f\u7684\u6b63\u78ba\u6027\u3002<\/p>\n<p><!--more--><\/p>\n<p>TypeScript \u4e2d\u7684\u7c7b\u578b\u5b88\u536b\u51fd\u6570\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u8b8a\u91cf\u7684\u985e\u578b\uff0c\u4ee5\u78ba\u4fdd\u7a0b\u5e8f\u7684\u6b63\u78ba\u6027\u3002\u7c7b\u578b\u5b88\u536b\u51fd\u6570\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u8b8a\u91cf\u662f\u5426\u70ba\u67d0\u500b\u985e\u578b\uff0c\u5982\u679c\u662f\uff0c\u5247\u8fd4\u56de true\uff0c\u5426\u5247\u8fd4\u56de false\u3002<\/p>\n<pre class=\"brush: typescript\">\nfunction isString(value: any): value is string {\n  return typeof value === 'string';\n}\n\nlet value: any = 'Hello World';\n\nif (isString(value)) {\n  console.log(value.toUpperCase());\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba isString \u7684\u51fd\u6578\uff0c\u5b83\u63a5\u53d7\u4e00\u500b\u53c3\u6578 value\uff0c\u4e26\u6aa2\u67e5\u5b83\u662f\u5426\u70ba\u5b57\u7b26\u4e32\u985e\u578b\uff0c\u5982\u679c\u662f\uff0c\u5247\u8fd4\u56de true\uff0c\u5426\u5247\u8fd4\u56de false\u3002\u7136\u5f8c\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u8b8a\u91cf value\uff0c\u4e26\u5c07\u5176\u8a2d\u7f6e\u70ba\u5b57\u7b26\u4e32 &#8216;Hello World&#8217;\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u4f7f\u7528 if \u8a9e\u53e5\u4f86\u6aa2\u67e5 value \u662f\u5426\u70ba\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u662f\uff0c\u5247\u5c07\u5176\u8f49\u63db\u70ba\u5927\u5beb\u5b57\u6bcd\uff0c\u4e26\u5c07\u5176\u8f38\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<p>TypeScript \u4e2d\u7684\u7c7b\u578b\u5b88\u536b\u51fd\u6570\u53ef\u4ee5\u8b93\u6211\u5011\u66f4\u52a0\u7cbe\u78ba\u5730\u6aa2\u67e5\u8b8a\u91cf\u7684\u985e\u578b\uff0c\u4ee5\u78ba\u4fdd\u7a0b\u5e8f\u7684\u6b63\u78ba\u6027\u3002\u6b64\u5916\uff0c\u6211\u5011\u9084\u53ef\u4ee5\u81ea\u5b9a\u7fa9\u7c7b\u578b\u5b88\u536b\u51fd\u6570\uff0c\u4ee5\u6eff\u8db3\u6211\u5011\u7684\u7279\u5b9a\u9700\u6c42\u3002<\/p>\n<pre class=\"brush: typescript\">\nfunction isNumber(value: any): value is number {\n  return typeof value === 'number';\n}\n\nlet value: any = 10;\n\nif (isNumber(value)) {\n  console.log(value * 2);\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba isNumber \u7684\u51fd\u6578\uff0c\u5b83\u63a5\u53d7\u4e00\u500b\u53c3\u6578 value\uff0c\u4e26\u6aa2\u67e5\u5b83\u662f\u5426\u70ba\u6578\u5b57\u985e\u578b\uff0c\u5982\u679c\u662f\uff0c\u5247\u8fd4\u56de true\uff0c\u5426\u5247\u8fd4\u56de false\u3002\u7136\u5f8c\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u8b8a\u91cf value\uff0c\u4e26\u5c07\u5176\u8a2d\u7f6e\u70ba\u6578\u5b57 10\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u4f7f\u7528 if \u8a9e\u53e5\u4f86\u6aa2\u67e5 value \u662f\u5426\u70ba\u6578\u5b57\uff0c\u5982\u679c\u662f\uff0c\u5247\u5c07\u5176\u4e58\u4ee5 2\uff0c\u4e26\u5c07\u7d50\u679c\u8f38\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<p>\u7e3d\u7d50\uff0cTypeScript \u4e2d\u7684\u7c7b\u578b\u5b88\u536b\u51fd\u6570\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u8b8a\u91cf\u7684\u985e\u578b\uff0c\u4ee5\u78ba\u4fdd\u7a0b\u5e8f\u7684\u6b63\u78ba\u6027\u3002\u6b64\u5916\uff0c\u6211\u5011\u9084\u53ef\u4ee5\u81ea\u5b9a\u7fa9\u7c7b\u578b\u5b88\u536b\u51fd\u6570\uff0c\u4ee5\u6eff\u8db3\u6211\u5011\u7684\u7279\u5b9a\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aTypeScript\u7c7b\u578b\u5b88\u536b\u51fd\u6570\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7a0b\u5f0f\u78bc\u4e2d\u66f4\u52a0\u7cbe\u78ba\u5730\u63a7\u5236\u8cc7\u6599\u985e\u578b\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5229\u7528TypeScript\u7c7b\u578b\u5b88\u536b\u51fd\u6570\u81ea\u5b9a\u4e49\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u5730\u958b\u767c\u51fa\u66f4\u512a\u8cea\u7684\u7a0b\u5f0f\u78bc\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-4870","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\/4870","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=4870"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4870\/revisions"}],"predecessor-version":[{"id":4871,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4870\/revisions\/4871"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}