{"id":4858,"date":"2023-01-05T11:05:41","date_gmt":"2023-01-05T03:05:41","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4858"},"modified":"2023-01-05T11:05:41","modified_gmt":"2023-01-05T03:05:41","slug":"%e4%ba%86%e8%a7%a3typescript%e7%b1%bb%e5%9e%8b%e8%b0%93%e8%af%8d%e4%bf%9d%e6%8a%a4%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e5%84%aa%e5%8b%a2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3typescript%e7%b1%bb%e5%9e%8b%e8%b0%93%e8%af%8d%e4%bf%9d%e6%8a%a4%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e5%84%aa%e5%8b%a2\/","title":{"rendered":"\u4e86\u89e3TypeScript\u7c7b\u578b\u8c13\u8bcd\u4fdd\u62a4\u7684\u529f\u80fd\u8207\u512a\u52e2"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, TypeScript \u7c7b\u578b\u8c13\u8bcd\u4fdd\u62a4, TypeScript type predicate protection\"><\/p>\n<h1>TypeScript \u7c7b\u578b\u8c13\u8bcd\u4fdd\u62a4(type predicate protection)<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e\u7531 Microsoft \u958b\u767c\u7684 JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u985e\u578b\u7cfb\u7d71\uff0c\u53ef\u4ee5\u5728\u7de8\u8b6f\u6642\u671f\u6aa2\u67e5\u932f\u8aa4\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u4e9b\u985e\u578b\u5b89\u5168\u7684\u529f\u80fd\u3002\u5176\u4e2d\u4e00\u500b\u529f\u80fd\u5c31\u662f TypeScript \u7c7b\u578b\u8c13\u8bcd\u4fdd\u62a4\uff0c\u5b83\u53ef\u4ee5\u5728\u7de8\u8b6f\u6642\u671f\u6aa2\u67e5\u985e\u578b\u662f\u5426\u7b26\u5408\u9810\u671f\uff0c\u4ee5\u78ba\u4fdd\u7a0b\u5f0f\u78bc\u7684\u6b63\u78ba\u6027\u3002<\/p>\n<p><!--more--><\/p>\n<p>TypeScript \u7c7b\u578b\u8c13\u8bcd\u4fdd\u62a4\u662f\u4e00\u7a2e\u985e\u578b\u5b89\u5168\u7684\u6280\u8853\uff0c\u5b83\u53ef\u4ee5\u5728\u7de8\u8b6f\u6642\u671f\u6aa2\u67e5\u985e\u578b\u662f\u5426\u7b26\u5408\u9810\u671f\uff0c\u4ee5\u78ba\u4fdd\u7a0b\u5f0f\u78bc\u7684\u6b63\u78ba\u6027\u3002\u5b83\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u51fd\u6578\u7684\u53c3\u6578\u662f\u5426\u7b26\u5408\u9810\u671f\uff0c\u4ee5\u53ca\u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u5426\u7b26\u5408\u9810\u671f\u3002<\/p>\n<p>TypeScript \u7c7b\u578b\u8c13\u8bcd\u4fdd\u62a4\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: javascript\">\nfunction isString(x: any): x is string {\n    return typeof x === 'string';\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u78bc\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba isString \u7684\u51fd\u6578\uff0c\u5b83\u63a5\u53d7\u4e00\u500b\u53c3\u6578 x\uff0c\u4e26\u6aa2\u67e5 x \u662f\u5426\u70ba\u5b57\u7b26\u4e32\u985e\u578b\uff0c\u5982\u679c\u662f\uff0c\u5247\u8fd4\u56de true\uff0c\u5426\u5247\u8fd4\u56de false\u3002<\/p>\n<p>TypeScript \u7c7b\u578b\u8c13\u8bcd\u4fdd\u62a4\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u51fd\u6578\u7684\u53c3\u6578\u662f\u5426\u7b26\u5408\u9810\u671f\uff0c\u4ee5\u53ca\u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u5426\u7b26\u5408\u9810\u671f\u3002\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 TypeScript \u7c7b\u578b\u8c13\u8bcd\u4fdd\u62a4\u4f86\u6aa2\u67e5\u51fd\u6578\u7684\u53c3\u6578\u662f\u5426\u70ba\u5b57\u7b26\u4e32\u985e\u578b\uff1a<\/p>\n<pre class=\"brush: javascript\">\nfunction doSomething(x: any) {\n    if (isString(x)) {\n        \/\/ do something with x\n    }\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 isString \u51fd\u6578\u4f86\u6aa2\u67e5\u53c3\u6578 x \u662f\u5426\u70ba\u5b57\u7b26\u4e32\u985e\u578b\uff0c\u5982\u679c\u662f\uff0c\u5247\u57f7\u884c\u76f8\u61c9\u7684\u64cd\u4f5c\u3002<\/p>\n<p>TypeScript \u7c7b\u578b\u8c13\u8bcd\u4fdd\u62a4\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u5426\u7b26\u5408\u9810\u671f\u3002\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 TypeScript \u7c7b\u578b\u8c13\u8bcd\u4fdd\u62a4\u4f86\u6aa2\u67e5\u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u5426\u70ba\u5b57\u7b26\u4e32\u985e\u578b\uff1a<\/p>\n<pre class=\"brush: javascript\">\nfunction getString(): string | undefined {\n    \/\/ ...\n}\n\nfunction doSomething() {\n    const x = getString();\n    if (isString(x)) {\n        \/\/ do something with x\n    }\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 isString \u51fd\u6578\u4f86\u6aa2\u67e5 getString \u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u5426\u70ba\u5b57\u7b26\u4e32\u985e\u578b\uff0c\u5982\u679c\u662f\uff0c\u5247\u57f7\u884c\u76f8\u61c9\u7684\u64cd\u4f5c\u3002<\/p>\n<p>TypeScript \u7c7b\u578b\u8c13\u8bcd\u4fdd\u62a4\u662f\u4e00\u7a2e\u975e\u5e38\u6709\u7528\u7684\u6280\u8853\uff0c\u5b83\u53ef\u4ee5\u5728\u7de8\u8b6f\u6642\u671f\u6aa2\u67e5\u985e\u578b\u662f\u5426\u7b26\u5408\u9810\u671f\uff0c\u4ee5\u78ba\u4fdd\u7a0b\u5f0f\u78bc\u7684\u6b63\u78ba\u6027\u3002\u5b83\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u51fd\u6578\u7684\u53c3\u6578\u662f\u5426\u7b26\u5408\u9810\u671f\uff0c\u4ee5\u53ca\u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u5426\u7b26\u5408\u9810\u671f\uff0c\u5f9e\u800c\u78ba\u4fdd\u7a0b\u5f0f\u78bc\u7684\u6b63\u78ba\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:TypeScript\u7c7b\u578b\u8c13\u8bcd\u4fdd\u62a4\u662f\u4e00\u7a2e\u6280\u8853\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u4eba\u54e1\u66f4\u6709\u6548\u5730\u6aa2\u67e5\u548c\u9a57\u8b49\u4ee3\u78bc\uff0c\u4ee5\u78ba\u4fdd\u4ee3\u78bc\u7684\u6b63\u78ba\u6027\u548c\u53ef\u9760\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39TypeScript\u7c7b\u578b\u8c13\u8bcd\u4fdd\u62a4\u7684\u529f\u80fd\uff0c\u4ee5\u53ca\u5b83\u5982\u4f55\u5e6b\u52a9\u958b\u767c\u4eba\u54e1\u66f4\u6709\u6548\u5730\u6aa2\u67e5\u548c\u9a57\u8b49\u4ee3\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-4858","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-1gm","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4858","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=4858"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4858\/revisions"}],"predecessor-version":[{"id":4859,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4858\/revisions\/4859"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}